Solved

Inheritance Problem ][

Posted on 1998-06-13
6
185 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now