471,613 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Array of object from varius subclasses

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
8 2851
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
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

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

Similar topics

5 posts views Thread by Martin Magnusson | last post: by
6 posts views Thread by billy | last post: by
9 posts views Thread by GiantCranesInDublin | last post: by
5 posts views Thread by JH | last post: by
4 posts views Thread by Technics | last post: by
14 posts views Thread by sumsin | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.