K
Kira Yamato
Two questions concerning overriding virtual functions. First question
should have a clear answer, while the second question is more of an
opinion question.
1) Does the C++ standard say anything regarding changing the access
scope when overriding virtual functions?
For example:
class A { protected: virtual void foo(); };
class B : public A { public: virtual void foo(); };
class C : public A { private: virtual void foo(); };
I've tested that in g++ 4.0.1 the compiler does not complain at all.
But that just may be an implementation thing.
Of course, I am aware of private and protected inheritance, which will
implicitly demote the access scope of every base methods in the derived
class. So, I would think that demote access scope has a well-defined
behavior in the C++ standard.
But what about promoting access scope? Is it ever a dangerous thing to do?
2) From a software design perspective, is it ever a good idea to
promote or demote the access scope of a virtual function? Or is it
like one of those "permissible but not beneficial" features of C++?
In java, you can only promote access scope, i.e., you cannot decrease
the accessibility of a method in the derived class that overrides one
in the base class. Of course, that is only because of the way java's
security model works.
Thanks for any input.
should have a clear answer, while the second question is more of an
opinion question.
1) Does the C++ standard say anything regarding changing the access
scope when overriding virtual functions?
For example:
class A { protected: virtual void foo(); };
class B : public A { public: virtual void foo(); };
class C : public A { private: virtual void foo(); };
I've tested that in g++ 4.0.1 the compiler does not complain at all.
But that just may be an implementation thing.
Of course, I am aware of private and protected inheritance, which will
implicitly demote the access scope of every base methods in the derived
class. So, I would think that demote access scope has a well-defined
behavior in the C++ standard.
But what about promoting access scope? Is it ever a dangerous thing to do?
2) From a software design perspective, is it ever a good idea to
promote or demote the access scope of a virtual function? Or is it
like one of those "permissible but not beneficial" features of C++?
In java, you can only promote access scope, i.e., you cannot decrease
the accessibility of a method in the derived class that overrides one
in the base class. Of course, that is only because of the way java's
security model works.
Thanks for any input.