C++ virtual void funcFoo() const = 0 和 virtual void funcFoo() = 0 之间的区别;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9974329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:27:41  来源:igfitidea点击:

Difference between virtual void funcFoo() const = 0 and virtual void funcFoo() = 0;

c++inheritance

提问by vgonisanz

I have a declaration in a cpp where a function is like:

我在 cpp 中有一个声明,其中一个函数类似于:

virtual void funcFoo() const = 0;

I assume that can be inherited by another class if is declared explicit, but what's the difference between

我假设如果声明为显式,则可以由另一个类继承,但是两者之间有什么区别

virtual void funcFoo() = 0;

Is important to me improve my programming and i want to know the difference. I don't want a malfunction caused by a bad inherit.

对我来说改进我的编程很重要,我想知道其中的区别。我不希望因继承不当而导致故障。

Thanks in advance.

提前致谢。

回答by juanchopanza

The first signature means the method can be called on a const instance of a derived type. The second version cannot be called on const instances. They are different signatures, so by implementing the second, you are not implementing or overriding the first version.

第一个签名意味着可以在派生类型的 const 实例上调用该方法。不能在 const 实例上调用第二个版本。它们是不同的签名,因此通过实现第二个版本,您不会实现或覆盖第一个版本。

struct Base {
   virtual void foo() const = 0;
};

struct Derived : Base {
   void foo() { ... } // does NOT implement the base class' foo() method.
};

回答by felipe

virtual void funcFoo() const = 0;
- You can't change the state of the object
- You can call this function via const objects
- You can only call another const member functions on this object

virtual void funcFoo() = 0;
- You can change the state of the object
- You can't call this function via const objects

The best tutorial or Faq I've seen about const correctectness was the C++ FAQ by parashift:

我见过的关于常量正确性的最佳教程或常见问题解答是 parashift 的 C++ FAQ:

Take a look: http://www.parashift.com/c++-faq-lite/const-correctness.html

看一看:http: //www.parashift.com/c++-faq-lite/const-correctness.html

回答by Péter T?r?k

The difference is that the first function can be called on constobjects, while the second can't. Moreover, the first function can only call other constmember functions on the same object. Regarding inheritance, they behave the same way.

不同之处在于第一个函数可以在const对象上调用,而第二个则不能。而且,第一个函数只能调用const同一对象上的其他成员函数。关于继承,它们的行为方式相同。

See also the C++ FAQon this topic.

另请参阅有关此主题的C++ 常见问题解答

回答by Konrad

The two virtual functions have different signatures but inheriting will work the same way. The former is a const method and can only support const operations (methods) and objects.

这两个虚函数具有不同的签名,但继承的工作方式相同。前者是const方法,只能支持const操作(方法)和对象。

回答by AlexTheo

const methods can not change the state of the object so the

const 方法不能改变对象的状态,所以

virtual void funcFoo() const = 0;

will be called in const instances of this class with the difference of

将在此类的 const 实例中调用,区别在于

virtual void funcFoo() = 0;

which could be called only in non constant instances. Try to google for the const logic in c++. http://en.wikipedia.org/wiki/Const-correctness

只能在非常量实例中调用。尝试在 C++ 中搜索 const 逻辑。 http://en.wikipedia.org/wiki/Const-correctness