Is there a possibility of (or fast workaround for) creating an object defined as derived a class without creating base class object in memory; instead the derived object should refer to the actually existing object of base class ("take-over" its memory residence)? This is needed for speed reasons - creating new a derived object, copying data from base class object to it, and then destroying开发者_运维问答 base object takes too much time.
You might want to consider composition instead of inheritance in this case - it would be more natural.
I wouldn't use the class
construct that is supported by the language. If you need something small and that flexible consider writing a struct
and implementing your own v-table
's using function pointers. Much the same way as this is done for example in the Linux kernel. Note that object oriented programming can be done in almost any language, not necessarily one that supports it.
You could then switch the v-table pointer on the fly and possibly perform some realloc
in order to add the fields that are required by the derived type.
In the end you could package all of this in a regular class
that doesn't have any dynamic methods and just delegates all the calls to the described internal structure. This shouldn't impose any memory or computational overhead.
EDIT: Actually I guess realloc
is not the way to go. This is a routine that engages the underlying operating system and requires a context switch. Almost always calling copy
will be faster provided you have the appropriate memory block already allocated. If you're interested in speed, then maybe consider also implementing your own memory management or using one of the alternative implementation provided by libraries such as boost.
I don't think you can do what you seem to want to do.
Consider:
d1 = prestochango(b);
d2 = prestochango(b);
d1.blarf = waldo;
// what value does d2.blarf now have?
Either d1 and d2 are distinct objects, including distinct b-substrates, or they are the same object.
Now, you MIGHT be able to FAKE it by making your b-substrate a static member of your d class.
If you want dynamic call derived class, the v-table is indispensable.
So maybe you can implement the "base class" with no data member, and pass in data or pointer of data that your virtual functions need as argument during calling.
It'll save memory but cost more time.
If you wish to create one version of a base class and have all objects inherit or be derived from the same instance, then you can declare things as static in the base class. Static means one version of it for every instance of a class.
i.e.
class FooBase {
protected:
static int IDCnt;
static int ObjCnt;
int ID;
public:
FooBase();
~FooBase();
virtual int GetID();
virtual int GetObjCnt();
virtual int GetIDCnt();
};
//implementation
int FooBase::IDCnt = 0; //need to init static vars
int FooBase::ObjCnt = 0;
FooBase::FooBase() { ID = IDCnt; IDCnt++; ObjCnt++; }
FooBase::~FooBase() { ObjCnt--; }
int FooBase::GetID() { return ID; }
int FooBase::GetObjCnt() { return ObjCnt; }
int FooBase::GetIDCnt() { return IDCnt; }
#include "FooBase.h"
class FooDerived : public FooBase {
//blah
};
#include "FooDervied.h"
int main() {
FooDerived A;
FooDerived B;
cout << A.GetID() << ' ' << A.GetObjCnt() << ' ' << A.GetIDCnt() << endl;
cout << B.GetID() << ' ' << B.GetObjCnt() << ' ' << B.GetIDCnt() << endl;
if(true) {
FooDerived C;
cout << A.GetObjCnt() << ' ' << A.GetIDCnt() << ' ' << B.GetObjCnt << C.GetIDCnt() << endl;
}
cout << B.GetObjCnt() << '' << A.GetObjCnt() << ' ' << A.GetIDCnt() << ' ' << B.GetIDCnt << endl;
}
In this manner you don't declare a base class item, instead, the instance of base class is inherited through the static variables which basically means all FooDerived
are looking at the same block of memory for FooBase::IDCnt
and FooBase::ObjCnt
.
Did you consider Factory design pattern ?
Your base class only needs to know what kinda derived class you wanna create
精彩评论