Solved

Inheritance Problem ][

Posted on 1998-06-13
6
189 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to become programmer? 12 33
C#  WebForms or MVC 4 105
What is the Best Editor for PHP Development ? 5 67
Issues with C++ Class 19 81
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. …
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 use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

867 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

22 Experts available now in Live!

Get 1:1 Help Now