By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Array of object from varius subclasses

P: n/a
Hi all,

I have a base class and some subclasses; I need to define an array of
objects from these various subclasses. What I have is something like:

{

//I have a base class, something like:

class CPeople {
public:
virtual void Input() {printf("Inside CPeople::Input()\n");}
virtual void Show() {printf("Inside CPeople::Show()\n");}
};

//and some subclasses

class CMale : public CPeople {
public:
void Input() {};
void Show() {};
};

class CFemale : public CPeople {
public:
void Input() {printf("Inside CFemale::Input()\n");}
void Show() {printf("Inside CFemale::Show()\n");}
};

//Now I need an array of people...

CPeople MyArray[10];

//I create a CFemale Object...

CFemale temp;
temp.Input();

//I assign this object to an array element:

MyArray[1] = temp;
MyArray[1].Show();

return 0;
}
// I get :
// "Inside CFemale::Input()"
// "Inside CPeople::Show()"

So when I call MyArray[1].Show() the function called is the one
defined in the base class ( CPeople::Show() ).

What do I need to do to get the desired behaviour?

Thank you in advance...
Marco
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Marco wrote:

What do I need to do to get the desired behaviour?


You defined an array of CPeople objects. No matter what objects you assign
to them, they won't ever turn into anything else.

You only get polymorphic behaviour (which is what you supposedly meant by
"desired"), when you invoke an object's virtual member function
through a pointer or a reference to a base class.

For example:

CPeople* MyArray[10];

MyArray[1] = new CFemale;
MyArray[1]->Show();

Max
Jul 22 '05 #2

P: n/a
Hi Max,

Ok, I got it... Thank you!

Marco

"Max M." <ed***@maxim.comm2000.it> ha scritto nel messaggio
news:br**********@newsreader.mailgate.org...
Marco wrote:

What do I need to do to get the desired behaviour?


You defined an array of CPeople objects. No matter what objects you assign
to them, they won't ever turn into anything else.

You only get polymorphic behaviour (which is what you supposedly meant by
"desired"), when you invoke an object's virtual member function
through a pointer or a reference to a base class.

For example:

CPeople* MyArray[10];

MyArray[1] = new CFemale;
MyArray[1]->Show();

Max

Jul 22 '05 #3

P: n/a

"Marco" <hw***********@tin.it> wrote in message
news:hC******************@tornado.fastwebnet.it...
Hi Max,

Ok, I got it... Thank you!

Marco

"Max M." <ed***@maxim.comm2000.it> ha scritto nel messaggio
news:br**********@newsreader.mailgate.org...
Marco wrote:

What do I need to do to get the desired behaviour?


You defined an array of CPeople objects. No matter what objects you assign to them, they won't ever turn into anything else.

You only get polymorphic behaviour (which is what you supposedly meant by "desired"), when you invoke an object's virtual member function
through a pointer or a reference to a base class.

For example:

CPeople* MyArray[10];

MyArray[1] = new CFemale;
MyArray[1]->Show();

Max



Note that Max made the change from an array of CPeople to an array of
CPeople*. Without this change, inserting an object of a subclasss with a
larger size would corrupt your memory.

Tom
Jul 22 '05 #4

P: n/a
Thomas Wintschel wrote:
Note that Max made the change from an array of CPeople to an array of
CPeople*.**Without*this*change,*inserting*an*objec t*of*a*subclasss*with*a
larger size would corrupt your memory.


Tom, this sentence doesn't make much sense. One cannot *insert* an object
into an array. Rather, one can use the assignment operator to change an
element's value. Marco's original code was perfectly legal and had well
defined behaviour, though different from what he expected.

Max
Jul 22 '05 #5

P: n/a
"Max M." <ed***@maxim.comm2000.it> wrote in message
news:br**********@newsreader.mailgate.org...
Thomas Wintschel wrote:
Note that Max made the change from an array of CPeople to an array of
CPeople*. Without this change, inserting an object of a subclasss with a
larger size would corrupt your memory.


Tom, this sentence doesn't make much sense. One cannot *insert* an object
into an array. Rather, one can use the assignment operator to change an
element's value. Marco's original code was perfectly legal and had well
defined behaviour, though different from what he expected.

Max


Apologies. I should not have used the word 'insert' and duly chastise
myself.

I was hoping to help him avoid running into any unexpected behaviour by
pointing out that instances of subclasses with additional data members could
not safely be stored in an array of base class objects, as in the following
example.

class Small
{
public:
Small() : m_n1(1) {};
Small& operator = (const Small &rhs)
{
m_n1 = rhs.m_n1;
return *this;
}
int Getn1() { return m_n1; }
void Setn1(int n) { m_n1 = n; }
private:
int m_n1;
};

// Make 'Big' twice the size of a 'Small'
class Big : public Small
{
public:
Big() : m_n2(2) {};
Big& operator = (const Big &rhs)
{
Small::operator=(rhs);
m_n2 = rhs.m_n2;
return *this;
}
int Getn2() { return m_n2; }
void Setn2(int n) { m_n2 = n; }
private:
int m_n2;
};

int main()
{
Small smalls[2];

Big big;
smalls[0] = big; // Only the 'Small' part of big is copied

// Unsafe, smalls[0] contains a 'Small', not a 'Big'
Big* pBig = static_cast< Big* >(&smalls[0]);

// Returns 1, as expected
int n1 = pBig->Getn1();

// Also returns 1, since it reading the value from
// the location where smalls[1] is stored
int n2 = pBig->Getn2();

// Modifying the subclass now affects the subsequent element in the
array
pBig->Setn2(3);
// Which now contains the value 3 instead of 1
n1 = smalls[1].Getn1();

// Extra dangerous, since it goes past the end of the array
pBig = static_cast< Big* >(&smalls[1]);
// Returns 3 as the result of previous manipulations
n1 = pBig->Getn1();
// Returns ?
n2 = pBig->Getn2();
}
Jul 22 '05 #6

P: n/a
On Sun, 14 Dec 2003 02:08:56 +0100, "Max M." <ed***@maxim.comm2000.it>
wrote:
Thomas Wintschel wrote:
Note that Max made the change from an array of CPeople to an array of
CPeople*.**Without*this*change,*inserting*an*objec t*of*a*subclasss*with*a
larger size would corrupt your memory.


Tom, this sentence doesn't make much sense. One cannot *insert* an object
into an array. Rather, one can use the assignment operator to change an
element's value. Marco's original code was perfectly legal and had well
defined behaviour, though different from what he expected.


He was referring to slicing which, although it might not "corrupt"
memory here, will indeed cause memory leaks.
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #7

P: n/a
Hi,

Thank you for all your replies and the information!

Marco
"Bob Hairgrove" <wouldnt_you_like@to_know.com> ha scritto nel messaggio
news:3f**************@news.webshuttle.ch...
On Sun, 14 Dec 2003 02:08:56 +0100, "Max M." <ed***@maxim.comm2000.it>
wrote:
Thomas Wintschel wrote:
Note that Max made the change from an array of CPeople to an array of
CPeople*. Without this change, inserting an object of a subclasss with a larger size would corrupt your memory.


Tom, this sentence doesn't make much sense. One cannot *insert* an object
into an array. Rather, one can use the assignment operator to change an
element's value. Marco's original code was perfectly legal and had well
defined behaviour, though different from what he expected.


He was referring to slicing which, although it might not "corrupt"
memory here, will indeed cause memory leaks.
--
Bob Hairgrove
No**********@Home.com

Jul 22 '05 #8

P: n/a
Bob Hairgrove wrote:

He was referring to slicing which, although it might not "corrupt"
memory here, will indeed cause memory leaks.


I don't get you. In what circumstances could slicing result in a memory
leak? Are you implying the code from Marco's first post (which assigned
derived-class objects to base-class ones) causes memory leaking? It does
not, actually.

Max
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.