Assigning class instance to an array doubt

S

Sree

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
 
K

Karl Heinz Buchegger

Sree said:
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 ?

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.
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 ?

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)
 
J

JKop

Karl Heinz Buchegger posted:
Sree said:
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 ?

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.
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 ?

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)

Well just in case you already knew that:


A a_object;

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


-JKop
 
P

Patrik Stellmann

Sree said:
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
As already posted that's not possible, but maybe using pointers helps?

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

Karl Heinz Buchegger

JKop said:
Karl Heinz Buchegger posted:
Sree said:
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 ?

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.
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 ?

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)

Well just in case you already knew that:

A a_object;

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

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.
 
K

Karl Heinz Buchegger

Patrik said:
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
As already posted that's not possible, but maybe using pointers helps?

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

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

DaKoadMunky

Well just in case you already knew that:
A a_object;

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

I don't understand.

What is the point to be illustrated by this posting?
 
K

Karl Heinz Buchegger

DaKoadMunky said:
Well just in case you already knew that:


A a_object;

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

I don't understand.

What is the point to be illustrated by this posting?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top