473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2992
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3012
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
5
6140
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
6
2355
by: billy | last post by:
I've got a set of subclasses that each derive from a common base class. What I'd like to do is create a global array of the class types (or, class names) that a manager class can walk through in...
11
3795
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
6290
by: GiantCranesInDublin | last post by:
Hi, I am looking for the best performing solution for modifying and iterating an object graph in JavaScript. I have outlined below a simplified example of the object model and examples of how I...
3
2031
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
5
3155
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
4
7349
by: Technics | last post by:
Ok I will be as clearer as I can (sorry for english/technical mistakes) I would like to write an audio application that supports ASIO drivers. I downloaded the ASIO sdk from Stainberg and I read...
14
1811
by: sumsin | last post by:
From 'Inside the C++ Object Model' by 'Stanley B. Lippman' 'The primary strength of the C++ Object Model is its space and runtime efficiency. Its primary drawback is the need to recompile...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
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
7319
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...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.