@All:
Thank you all for taking time to reply! Great answers!
I like the "contract" explanation.
However, here I'm just thinking a bit further:
We know that the latest C++ standard supports keyword "override". This means, the compiler knows who in the Derived class are the successors to the virtual functions in the Base class.
Both pieces below, which are declarations in header file, compile successfully:
1 2 3 4 5 6 7 8 9 10 11 12
|
class Base
{
public:
virtual void print ();
};
class Derived : public Base
{
public:
void print () override;
};
|
1 2 3 4 5 6 7 8 9 10 11 12
|
class Base
{
public:
virtual void print ();
};
class Derived : public Base
{
protected: // attention HERE: different access control
void print () override;
};
|
Since the compiler knows which Derived function is the successor to which Base function, I'm just thinking, it makes more sense for the compiler to enforce the access control at compilation.
Reason:
1, it is technically feasible (similar to implementation of keyword "override", isn't it?)
2, the Base class serves as a contract, and access control is certainly a part of contract.
What do you guys think? Please point out any mistakes or unreasonable inference of mine.
Thanks!