C++ allocate objects on heap of base class with protected constructors via inheritance
Posted
by
KRao
on Stack Overflow
See other posts from Stack Overflow
or by KRao
Published on 2010-12-25T18:49:49Z
Indexed on
2010/12/25
18:54 UTC
Read the original article
Hit count: 178
c++
|inheritance
I have a class with protected constructor:
class B {
protected:
B(){};
};
Now I derive from it and define two static functions and I manage to actually create objects of the class B, but not on the heap:
class A : public B {
public:
static B createOnStack() {return B();}
//static B* createOnHeap() {return new B;} //Compile time Error on VS2010
};
B b = A::createOnStack(); //This works on VS2010!
The question is: 1) Is VS2010 wrong in allowing the first case? 2) Is it possible to create objects of B without modifying B in any way (no friendship and no extra functions). I am asking, because it is possible to make something similar when dealing with instances of B and its member functions, see: http://accu.org/index.php/journals/296
Thank you in advance for any suggestion!
Kind regards
© Stack Overflow or respective owner