Solved

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

Posted on 2007-03-30
2
185 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

16 Experts available now in Live!

Get 1:1 Help Now