Solved

Inheritance Problem ][

Posted on 1998-06-13
6
215 Views
Last Modified: 2013-12-14
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
Comment
Question by:Sinclair
[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
  • 4
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1165817
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165818
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
 
LVL 2

Author Comment

by:Sinclair
ID: 1165819
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:nietod
ID: 1165820
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
 
LVL 22

Expert Comment

by:nietod
ID: 1165821
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
 
LVL 2

Author Comment

by:Sinclair
ID: 1165822
Thanks again, I will go convert my program now :-)
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

635 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