473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Assigning class instance to an array doubt


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/
Jul 22 '05 #1
7 1315
Sree wrote:

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)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Karl Heinz Buchegger posted:
Sree wrote:

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
Jul 22 '05 #3


Sree wrote:
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;

Jul 22 '05 #4
JKop wrote:

Karl Heinz Buchegger posted:
Sree wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
Patrik Stellmann wrote:

Sree wrote:
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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6
>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?


Jul 22 '05 #7
DaKoadMunky wrote:
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 :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: trinitypete | last post by:
Hi, This post is just to check that I am not missing a point somewhere. I have a class say 'CLASSA' with a string member field and associated property. I need an array of this class so I...
7
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other...
4
by: Mrinal Kamboj | last post by:
Hi , I had a doubt regarding a piece of code with me , that has to do with System.Threading.Thread class . In it user instantiates an array of Thread class and to all of them assign a method...
2
by: ryoung | last post by:
I receive the following error when I attempt to assign values to a web service array. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. ...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
11
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at...
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
2
by: alefajnie | last post by:
class A: this_is_original_variable_only_for_one_inctance = 0 def __init__(self, v): self.this_is_original_variable_only_for_one_inctance = v class B: this_is_common_for_all_instances =
1
by: The Pythonista | last post by:
I've been wondering for a while about whether assigning to __class__ is bad form or not. Specifically, I mean doing so when some other method of implementing the functionality you're after is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.