Invoking a protected constructor in a derived class is not allowed, as explained here.
The accepted answer explains that protected
grants access to members of an object of base class A
only when that object of class A
is a subobject of class B
. So far, so good.
But then, why is it allowed (at least in GCC 4.6.3) to call static protected methods? Specifically, it doesn't make any sense to me that the following compiles, while the commented line does not:
class A
{
protected:
A() {}
static A makeA() { return A(); }
};
class B: public A
{
public:
static A makeAFromB()
{
return makeA(); // compiles
// return A(); // does not compile
}
};
Philosophically, a constructor is pretty much like a static method returning an object of class A
, reason why I don't get the difference in behaviour here.