Hi,
All classes in one of my program derive from certain base class which overrides the new and delete operators. There are multiple such base classes depending upon the type of class deriving it. I am having trouble however with the design decision. Let me explain my problem with an example:
[source lang='C++']class ABase{public: void* operator new(size_t s) { return MemoryManagerForA::Alloc(s); } void operator delete(void* ptr) { MemoryManagerForA::Free(ptr); }};class BBase{public: void* operator new(size_t s) { return MemoryManagerForB::Alloc(s); } void operator delete(void* ptr) { MemoryManagerForB::Free(ptr); }};class A : public ABase{protected: int a;public: A() { a = 10; }};class B : public BBase{protected: int b;public: B() { b = 10; }};class C : public A, public B{ int c;public: C() { c = a + b; }};
The problem is with class C in the above code. There is a clear ambiguity as to which operator it must call. So if I write 'new C()' it will throw me a compilation error. Is there any workaround to this problem given I need this design pattern in which there must be multiple base classes for C?
Further I cannot inherit the base classes at level 'C'. (This design may be flawed because if I delete a pointer of class C type-casted as A or B, the proper free might not be called). I am not sure how to go around this problem.
Any help will be much appreciated.
Thanks,
obhi