Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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