getter 中的 C++ const

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21478342/
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 23:39:34  来源:igfitidea点击:

C++ const in getter

c++constgetter

提问by nkint

I'm still learning about C++ and I'm reading everywhere that I have to use consteverywhere I can (for speed reason I think).

我仍在学习 C++ 并且我正在阅读我必须在const任何地方使用的任何地方(我认为是出于速度原因)。

I'm usually write my getter method like this:

我通常这样写我的getter方法:

const bool isReady() {
    return ready;
}

But I've seen that some IDE autogenerate getter in this way:

但我已经看到一些 IDE 以这种方式自动生成 getter:

bool getReady() const {
    return ready;
}

But, writing delegates, it happened to me to find this error if the constis after the function:

但是,写委托时,如果const在函数之后,我碰巧发现了这个错误:

member function 'isReady' not viable: 'this' argument has type 'const VideoReader', but function is not marked const

So that, what is the better way to write a const getter? Do I really have to care about?

那么,编写 const getter 的更好方法是什么?我真的需要关心吗?

回答by mkaes

There is a huge difference between the two ways.

这两种方式有很大的不同。

const bool isReady()

The code above will return a const bool, but it does not guarantee that the object will not change its logic state.

上面的代码将返回 a const bool,但不保证该对象不会改变其逻辑状态。

bool isReady() const

This will return a bool, and it guarantees that the logic state of your object will not change. In this case it is not necessary to write constin front of the return type. It makes no sense to return a const boolbecause it is a copy anyway. So making it constis useless. The second constis needed for constcorrectness, which is not used for speed reasons but to make your program more reliable and safe.

这将返回 a bool,并保证您的对象的逻辑状态不会改变。在这种情况下,没有必要const在返回类型前面写。返回 a 是没有意义的,const bool因为它无论如何都是一个副本。所以制作它const是没有用的。第二个constconst正确性所必需的,它不是出于速度原因而使用的,而是为了使您的程序更可靠和安全。

回答by Diana

They mean two differnt things:

它们的意思是两个不同的东西:

const bool isReady() {
    return ready;
}

This returns a constant bool. Meaning a bool which cannot change value from the time it's been created.

这将返回一个常量布尔值。意思是一个布尔值,从它被创建时起就不能改变它的值。

bool getReady() const { 
    return ready;
}

This is a a constant function, meaning a function that will not alter any member variables of the class it belongs to. This is the style recommended to use for getters, since their only purpose is to retrieve data and should not modify anything in the process.

这是一个常量函数,意思是一个不会改变它所属类的任何成员变量的函数。这是推荐用于 getter 的样式,因为它们的唯一目的是检索数据并且不应在此过程中修改任何内容。

回答by marcinj

constmethod informs compiler that you will not modify class instance on which this method is called:

const方法通知编译器您不会修改调用此方法的类实例:

class A {
public:
bool getReady() const {
    return ready;
}
};

so if you try to modify your object inside getReady() then compiler will issue error. Const methods are usefull where you have ie.: const A&, or const A*, then you can call only const methods on such objects.

因此,如果您尝试在 getReady() 中修改对象,则编译器将发出错误。const 方法在你有的地方很有用,例如:const A& 或 const A*,那么你只能在这些对象上调用 const 方法。

as for:

至于:

const bool isReady() {
    return ready;
}

this const provides actually no real benefit, because bool is copied while isReady() returns. Such constwhould make sense if returned type was a const char*or const A&, in such cases constmakes your char string or A class instance immutable.

这个 const 实际上没有提供真正的好处,因为在 isReady() 返回时会复制 bool 。这样的const对子级意义,如果返回类型是const char*const A&,在这种情况下const让你的字符的字符串或一个类的实例是不可改变的。

回答by Luchian Grigore

A const getter has the signature

const getter 具有签名

bool getReady() const

The other version isn't a constmethod, it just returns a constvalue (which is basically useless).

另一个版本不是const方法,它只是返回一个const值(基本上没用)。

Having a constgetter allows you to call it on const objects:

拥有一个constgetter 允许你在 const 对象上调用它:

const Object obj;
obj.getReady();

This is only valid if getReadyis marked as const.

这仅在getReady标记为时才有效const

回答by mcserep

There is a difference between using the constkeyword for the return type or for the method signature. In the first case the returned value will be a constant value. In the second case the method will be a so-called constant method, which cannot change the representation of the object. On constant objects, only the constant methods are callable.

const关键字用于返回类型或用于方法签名是有区别的。在第一种情况下,返回值将是一个常量值。在第二种情况下,该方法将是所谓的常量方法,它不能改变对象的表示。在常量对象上,只有常量方法是可调用的。