override - Overriding public virtual functions with private functions in C++ -


Is there an override C ++ virtual function on a reason to create different permissions from the base class? Is there any danger in doing this?

For example:

  class base {public: virtual int foo (double) = 0; } Children of the class: Public base {Private: virtual Inc. foo (double); }  

Say that this is a bad idea, but why not say it.

I have seen this idiom in some code and I believe that the author is not possible to override any private member function, trying to make the class final based on a perception. However, shows an example of overriding private tasks. Certainly it is recommended to do this.

My solid question:

  1. Is there a technical problem in using separate permissions for virtual methods in the derived class vs. base class

  2. Is there any reason to do this?

The problem is that base class methods to declare its interface The way. It is briefly saying, "These are the things that you can do for the things of this class."

When you make something in a derivative class, you are declared publicly, you are taking some distance. Now, even if a derived object is "is-a" base object, something that you should be able to do with a base class object, which you can not do in a derived class object, then

Will be broken. Is there a "technical" problem in your program? Probably not. But this would mean that the objects of your classes will not behave in any way, the way your users expect them to behave.

If you see yourself in the situation where you want (in one case the disliked method is specified in another answer), it is likely that you have a succession model where the circumstances Actually does not have "A-A" modeling (for instance Scott Mers example square rectangle from Rectangle), but you can not change the width of the squares independent of its height as you can for a rectangle ) And you may need to rethink their class relations.


Comments