Sorry for the non-descript title, but I’m having a hard way with words right now. I’ll just jump into it:
class A
{
public:
protected:
int doSomething();
};
class B : public A
{
public:
int doSomething() { return A::doSomething(); }
};
...
A *a = new A;
B *b = static_cast<B*>(a);
cout << b->doSomething();
Notably, B inherits A and does not add any data members or virtual functions, and does not override any virtual functions. This means an instance of B should be equivalent in memory to an instance of A, I assume.
Basically, is this safe? I’m pretty sure it’s not legal, but is it portable? Are there any potential consequences of relying on something like this? (Other than the normal yadayada.)
Obviously this is bad practice, but I see it much like I see casting away const – it’s something that may be better than an alternative (if there is one.) However, if you have any non-obvious reasons it may be bad, I’m definitely interested.
Even if nothing obviously bad will happen, the question is, why would you be forced to do something like this?
If you only want a wrapper, it is much cleaner design to make A a member of B, and let B forward the calls to A.