How to make sure that you redefine a method in a derived class in C ++?

class Base { public: void foo() const { std::cout << "foo const" << std::endl; } }; class Derived : public Base { public: void foo() { std::cout << "foo"<< std::endl; } } 

I want to make sure that foo () const is correctly hidden for Base. Yes, this is a bad idea, and maybe I should make Base :: foo () const pure virtual so that Dervied :: foo () will correctly override - but let me say that I can not make base :: foo () const pure virtual. Is there a better way to make sure Base :: foo () const is correctly hidden in Derived?

Edit: I want to make sure that in Derived I correctly hid the underlying implementation.

+4
source share
6 answers

By simply defining the foo member function in the derived class, you hid all the foo functions in the base class.

You need to clarify the question a bit - are you worried that foo in a derived class is not a proper replacement for foo in a base class? I find it difficult to determine what you are really asking.

Edit: Based on your changes and additional comments, I think you have a misunderstanding of how work in C ++ is hiding. In this case, it does not matter that one function is constant and the other is not - as soon as C ++ finds the function foo in the derived class, it stops looking elsewhere! This is usually a huge trap for people. Consider the following:

 class Base { void foo(double d) { cout << d; } }; class Derived : public Base { void foo(int i) { cout << i; } }; Derived obj; obj.foo(123.456); 

What do you think, on the way out? This is 123! You probably received a compiler warning saying that a double value would be truncated. Despite the fact that the signature of a function that takes a double is obviously the best match, it was never taken into account - it was hidden.

+4
source

"void foo () const" and "void foo ()" are two completely different functions related to C ++. That is why you do not see Derived "foo" hiding "foo" from the base.

+1
source

Are you sure you want to HIDE foo () from Base? If you want to use polymorphism, you need to make the base version of foo () virtual. However, this should not be clean. Otherwise, you get static binding - do you want this?

+1
source

If you do not want to make it pure virtual and thus force the implementation, you can simply create the body foo() in the Base class immediately throw an exception. This serves to force any developers and users to implement the override or explode it when they try to use it.

I have seen this before, and although it is a little ugly, it works very well.

0
source

If I understand your question correctly, you ask how to make sure that Derived classes override the method forcibly without making the method clean in the Base class.

After making sure that all of your derived classes have provided an implementation of the method, IMHO you are hinting that the method in the Base class is not really used for any purpose. Objects must call any version of the Derived class of the foo() method. Therefore, in my opinion, the method should be pure virtual. I guess I don’t think of another way to achieve this.

Also note that in your example, you changed the signature of foo() in Base and Derived . This makes the Base class method invisible in the Derived class. If you are not using using Base::foo , the foo() method will not be visible to the Derived class.

0
source

In Base :: foo () you can do the following:

 assert (dynamic_cast<const Derived*> (this) == NULL); 

But he has three problems:

  • A change is required in the Base class, which must be closed for modification.
  • It may be stronger than you need, because you can allow Base :: foo () to be called from the base object or explicitly.
  • It uses RTTI, which means vtbl, which may be the reason you don't want user-defined virtual functions to start with.
0
source

All Articles