Solved

Using Dynamic Arrays, storing objects of different classes (which all are inherited from one base class)

Posted on 2007-03-30
2
186 Views
Last Modified: 2013-12-14
Hi,
I am having a problem regarding dynamic arrays, and storing different classes (which are all inherited off the same class) within them:

Firstly, before the code I am having problems with the two classes used in this example:

SCREEN_OBJECT - this stores all the variables etc. It also has a number of virtual functions, which then within other classes (e.g.) BUTTON, are then defined.

so, e.g.
class SCREEN_OBJECT
{
private:
int a,b,c;
public:
void virtual myfunc() {}
}

and then:
class BUTTON : public SCREEN_OBJECT
{
public:
void myfunc() { /*code*/  }
}

(and lots of other classes like BUTTON)

The basic problem is:

When I store them in a "normal"/static array it's fine. e.g.
-----------------------------------------------
SCREEN_OBJECT* my_array[5];
my_array[0] = p_BUTTON;
-----------------------------------------------

Here, it copies accorss p_BUTTON fine, all the vars, as well as the class type, so when calling my_array[0]->myfunc() it calls the correct function from the BUTTON class.
 
However if I am using a dynamic array. (which is what I really need to do).
------------------------------------------------------------------
int count = 5;
SCREEN_OBJECT* array = new SCREEN_OBJECT[count ];
my_array[0] = *p_BUTTON;
------------------------------------------------------------------

copies the vars accross from p_BUTTON into the array, except the class type, and hence then calling the virtual functions doesn't correctly call the function from the BUTTON class, but instead calls it from the SCREEN_OBJECT class.

I fully expect/realise that this is because I've defined all the objects in the array as a SCREEN_OBJECT ("new SCREEN_OBJECT"), however is there any way around this problem. Either by somehow else using a dynamic array, or when calling "my_array[0] = *p_BUTTON;" copying the object type as well?

Bearing in mind the actual array size is a lot larger than 5, and if it helps the only difference between BUTTON and my other class types is the code inside the functions, none of them have extra vars. (all the Vars are inside the base SCREEN_OBJECT class)

Thanks for reading any help you can offer
Fran
0
Comment
Question by:FC28
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 18825211
Make that

int count = 5;
SCREEN_OBJECT** array = new SCREEN_OBJECT*[count ];
my_array[0] = p_BUTTON;

and it will work. Your codes uses an array of instances, and you actually need an array of pointers. The array of instances will coerce your vutoons to the base class type, which causes that effect.
0
 

Author Comment

by:FC28
ID: 18825531
Thank you very much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now