Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Inheritance Problem ][

Ok, so maybe I do not know C++ as well as I thought I did...
I have code which resembles the following:

class Base // Abstract Class
{
public:
  unsigned long ulSize;

 Base(void):ulSize(0) {}
  //virtual functions...
};

class X:public Base
{
public:
  X(void):ulSize(sizeof(X)) {}
 // a bunch of new data members, overridden functions...
};  

class Y:public Base
{
public:
  Y(void):ulSize(sizeof(Y)) {}
 // a bunch of OTHER new data members, overridden functions...
};

I need to implement a function foo which will take an array of Base "objects" (obviously, the array will actually contain X, Y or some other derived objects), and the number of items in the array, and perform various operations on the array. So far, what I have come up with is the following:

class Array
{
public:
  Base *ptr;
  int count;
  unsigned long ulSize;

  Array(Base *aptr, int acount):ptr(aptr),count(acount)
  {
    ulSize = ptr[0].ulSize;
    assert(ulSize > 0);
  }

  Base &operator[](int index)
  {
    return *( (Base *)( (char *)ptr + ulSize*index) );
  }
};

I don't like this solution because it requires memory allocation/deallocation outside the Array class (since the class can't know what type to allocate). Is there a better solution ? I would like to stay away from linked lists, since my arrays will never exceed 1k, anyway...
0
Sinclair
Asked:
Sinclair
  • 4
  • 2
1 Solution
 
nietodCommented:
answer coming.
0
 
nietodCommented:
Your best bet is to make the array store and array of pointer to Base objects rather than store the objects directly.  

Advantages:  
You don't need to store the size of each object inside each object.  (There were ways around that with your current design, if you're interested).
The array could be used to store a mix of derived class objects, that is, some X's and some Y's.
The array could be implimented using one of the STL classes (or just simply be one of the sTL classes).
The array could be made to do a lot more manipulation without help from the caller.  For example, it could add new entries (with NULL pointers, or with pointers to default created objects.)

0
 
SinclairAuthor Commented:
Thanks, nietod ! I like this array thing, even though I would have to modify quite a lot of stuff... Out of curiosity, what was that other solution you mentioned ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nietodCommented:
Note the fact that the array that is internal to the Array class stored pointers rather than objects can be totally hidden from the Array class's clients.  The way that the Array class is implimneted is of no concern to the classes that use the array class.
0
 
nietodCommented:
Not another solution.  Another way to handle your current approach.  You could use a virutal function inside each class that returns the the class's size.  That would be a lot more memory efficient than storing the size of each object in the object.  

I tried approaches like this in the past and always found them lacking.   They can be made to work, but had too many annoying problems.
0
 
SinclairAuthor Commented:
Thanks again, I will go convert my program now :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now