Solved

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

Posted on 2007-03-30
2
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

615 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