Solved

Inheritance Problem ][

Posted on 1998-06-13
6
195 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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