Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-30
2
Medium Priority
?
192 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

670 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