3
votes

I need help with a specific programming problem in C++ (not sure if this is even possible in C++). I need to be able to access all public member functions in Base class, but do not want to allocate memory for the Base class data while allocating the Derived class object.

Lets say, I have:

class Base
{
  public:
    Base();
    ~Base();
    int GetFoo() { return foo; }
    // other public member functions of Base class

  private:
    int foo;
    // other data
};

class Derived : public Base
{
  public:
    Derived(Base *BasePtr);
    Derived(Base &BaseRef);
    ~Derived();
    double GetXyz() { return xyz; }
    // other public member functions of Derived class

  private:
    double xyz;
    // other data
};

Now, lets say I already have a Base class allocated and initialized. I want to create a new Derived class object by referring to that existing Base object and allocate memory only for the data specific to the Derived class. Following the above example, I would have already allocated memory for "int foo" in the Base class object and only want to allocate memory for "double xyz" in the Derived class object.

Base *basePtr = new Base();
Derived *derivedPtr = new Derived(basePtr); // what is the content of this function?

What should be the memory allocation or the constructor for the Derived class look-like? I want to inherit all data and member functions of the Base class, but without doing a "combined" data allocation of Base and Derived. I have tried overloading operator new but no luck. Any help is appreciated.

2
You might need a copy constructor then or don't create a Base class object instead call the constructor for Base class inside your derived classsmac89
Please see my comment to Answer 1 below, I do not have the choice of not creating Base objects upfront.user2998286

2 Answers

0
votes

I'm not sure if it fits your requirements, but you could simply copy the contents of the Base stored in basePtr inside a new Derived object, then delete the Base object and point at the new Derived object with the old Base pointer. Like this:

Base *basePtr = new Base();
/* Do something with base here */
Derived *derivedPtr = new Derived(basePtr);
delete basePtr;
basePtr = derivedPtr;
derivedPtr = 0;

That way you'll end up with only one object (of type Derived) and won't have to store a separate pointer to it, nor store the Base object, and that seems like what you need.

Update:

in my case, I cannot delete the Base object since I would have created millions of them (using GigaBytes of RAM space) and people could be using pointers/references to those objects, so copying them to Derived objects and deleting the old Base objects doesn't work for me.

In that case, maybe you should try to do an "extendable* structural wrapper. First create an simple class without any method or member:

class Additional{};

Then create a wrapper structure:

struct wrapper{
    Base *basePtr;
    Additional *moreInfo;
}

Then, instead of deriving your Derived class from Base, derive it from Additional. And instead of storing milions of Base pointers, store milions of wrapper pointers. IT'll make your code a bit longer and harder to understand, but it'll do what you need. Well - unless the only thing your Derived class adds is a pointer or any data that takes less size.

If you've got virtual functions in Base, in order to use them with the new hierarchy, oyu'll just have to check every time:

wrapper *wrapperPtr = &alreadyCreatedSomewhereWrapper;
if(wrapperPtr->moreInfo)
    wrapperPtr->moreInfo->function();
else
    wrapperPtr->basePtr->function();
0
votes

Use multiple inheritance.

class Base()
class Derive1() : Base 
class Derive2() : Base
class MostDerive() : Derive1 , Derive2

MostDerived() will be thin. Derive1 and Derive2 will need to be deterministically constructed, but that can be imposed, or some Init() functions can be used.