Connecting Tech Pros Worldwide Forums | Help | Site Map

Assigning class instance to an array doubt

Sree
Guest
 
Posts: n/a
#1: Jul 22 '05

If class B derives from class A and we create an array of class B like
B *arrayOfB = new B[10]; and
// Instance of A
A firstInstanceOfA;
How can we store firstInstanceOfA in arrayOfB ? I tried the following way
arrayOfB[0] = firstInstanceOfA;
arrayOfB[0] = (B*)firstInstanceOfA;
Both the ways doesn't seems to work, also if we need to reverse the way of
assigning, like
A *arrayOfA = new A[10];
and we need to store instance of B what has to be done ?
Thanks in Advance
Ik

--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/

Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Assigning class instance to an array doubt


Sree wrote:[color=blue]
>
> If class B derives from class A and we create an array of class B like
> B *arrayOfB = new B[10]; and
> // Instance of A
> A firstInstanceOfA;
> How can we store firstInstanceOfA in arrayOfB ?[/color]

You can't.
A 'B' object is an 'A' object also. But the reverse is not
true: an 'A' object is not a 'B' object.
[color=blue]
> I tried the following way
> arrayOfB[0] = firstInstanceOfA;
> arrayOfB[0] = (B*)firstInstanceOfA;
> Both the ways doesn't seems to work, also if we need to reverse the way of
> assigning, like
> A *arrayOfA = new A[10];
> and we need to store instance of B what has to be done ?[/color]

You can't either.
a 'B' object extends an 'A' object. So if you assign a 'B'
object to an A variable ...

B ObjB;
A[0] = ObjB;

.... the 'A-part' of B is extracted and stored in the variable.
We say: the B object has been sliced to an A object (it looses
all its B information, until just an A object is left)

--
Karl Heinz Buchegger
kbuchegg@gascad.at
JKop
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Assigning class instance to an array doubt


Karl Heinz Buchegger posted:
[color=blue]
> Sree wrote:[color=green]
>>
>> If class B derives from class A and we create an array of class B like
>> B *arrayOfB = new B[10]; and
>> // Instance of A
>> A firstInstanceOfA;
>> How can we store firstInstanceOfA in arrayOfB ?[/color]
>
> You can't.
> A 'B' object is an 'A' object also. But the reverse is not
> true: an 'A' object is not a 'B' object.
>[color=green]
>> I tried the following way
>> arrayOfB[0] = firstInstanceOfA;
>> arrayOfB[0] = (B*)firstInstanceOfA;
>> Both the ways doesn't seems to work, also if we need to reverse the
>> way of assigning, like
>> A *arrayOfA = new A[10];
>> and we need to store instance of B what has to be done ?[/color]
>
> You can't either.
> a 'B' object extends an 'A' object. So if you assign a 'B'
> object to an A variable ...
>
> B ObjB;
> A[0] = ObjB;
>
> ... the 'A-part' of B is extracted and stored in the variable.
> We say: the B object has been sliced to an A object (it looses
> all its B information, until just an A object is left)
>[/color]

Well just in case you already knew that:


A a_object;

arrayOfB[0] = static_cast<B*>(&a_object);


-JKop
Patrik Stellmann
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Assigning class instance to an array doubt




Sree wrote:[color=blue]
> If class B derives from class A and we create an array of class B like
> B *arrayOfB = new B[10]; and
> // Instance of A
> A firstInstanceOfA;
> How can we store firstInstanceOfA in arrayOfB ? I tried the following way
> arrayOfB[0] = firstInstanceOfA;
> arrayOfB[0] = (B*)firstInstanceOfA;
> Both the ways doesn't seems to work, also if we need to reverse the way of
> assigning, like
> A *arrayOfA = new A[10];
> and we need to store instance of B what has to be done ?
> Thanks in Advance
> Ik
>[/color]
As already posted that's not possible, but maybe using pointers helps?

A *arrayOfAPtrs[10];
B firstInstanceOfB;
arrayOfAPtrs[0] = &firstInstanceOfB;

Karl Heinz Buchegger
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Assigning class instance to an array doubt


JKop wrote:[color=blue]
>
> Karl Heinz Buchegger posted:
>[color=green]
> > Sree wrote:[color=darkred]
> >>
> >> If class B derives from class A and we create an array of class B like
> >> B *arrayOfB = new B[10]; and
> >> // Instance of A
> >> A firstInstanceOfA;
> >> How can we store firstInstanceOfA in arrayOfB ?[/color]
> >
> > You can't.
> > A 'B' object is an 'A' object also. But the reverse is not
> > true: an 'A' object is not a 'B' object.
> >[color=darkred]
> >> I tried the following way
> >> arrayOfB[0] = firstInstanceOfA;
> >> arrayOfB[0] = (B*)firstInstanceOfA;
> >> Both the ways doesn't seems to work, also if we need to reverse the
> >> way of assigning, like
> >> A *arrayOfA = new A[10];
> >> and we need to store instance of B what has to be done ?[/color]
> >
> > You can't either.
> > a 'B' object extends an 'A' object. So if you assign a 'B'
> > object to an A variable ...
> >
> > B ObjB;
> > A[0] = ObjB;
> >
> > ... the 'A-part' of B is extracted and stored in the variable.
> > We say: the B object has been sliced to an A object (it looses
> > all its B information, until just an A object is left)
> >[/color]
>
> Well just in case you already knew that:
>
> A a_object;
>
> arrayOfB[0] = static_cast<B*>(&a_object);[/color]

That's the equivalent of telling the compiler:
"Dear compiler. I know that those types don't fit. But
listen closely: I don't care. You better do what I tell
you to do, or I will switch of the power immdiatly"

And then the compiler has no other choice: He silently
does what you request it to do, even if it is plain wrong.

That's the culprit with casts. They are a way to simply
shut down the compiler and overrule everything. A cast
is a weapon. It has to be used wisely.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Karl Heinz Buchegger
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Assigning class instance to an array doubt


Patrik Stellmann wrote:[color=blue]
>
> Sree wrote:[color=green]
> > If class B derives from class A and we create an array of class B like
> > B *arrayOfB = new B[10]; and
> > // Instance of A
> > A firstInstanceOfA;
> > How can we store firstInstanceOfA in arrayOfB ? I tried the following way
> > arrayOfB[0] = firstInstanceOfA;
> > arrayOfB[0] = (B*)firstInstanceOfA;
> > Both the ways doesn't seems to work, also if we need to reverse the way of
> > assigning, like
> > A *arrayOfA = new A[10];
> > and we need to store instance of B what has to be done ?
> > Thanks in Advance
> > Ik
> >[/color]
> As already posted that's not possible, but maybe using pointers helps?
>
> A *arrayOfAPtrs[10];
> B firstInstanceOfB;
> arrayOfAPtrs[0] = &firstInstanceOfB;[/color]

That would do it.
It's the first step on the road to polymorphism.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
DaKoadMunky
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Assigning class instance to an array doubt


>Well just in case you already knew that:[color=blue]
>
>
>A a_object;
>
>arrayOfB[0] = static_cast<B*>(&a_object);[/color]

I don't understand.

What is the point to be illustrated by this posting?




Karl Heinz Buchegger
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Assigning class instance to an array doubt


DaKoadMunky wrote:[color=blue]
>[color=green]
> >Well just in case you already knew that:
> >
> >
> >A a_object;
> >
> >arrayOfB[0] = static_cast<B*>(&a_object);[/color]
>
> I don't understand.
>
> What is the point to be illustrated by this posting?[/color]

That, when the typo is fixed, you can force the compiler
to nearly accept everything by using casts :-)

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Closed Thread