相关疑难解决方法(0)

const-correctness的定义是什么?

我认为"const-correctness"的概念定义得很明确,但当我与其他人谈论它时,似乎我们对它的含义有不同的看法.有人说它是关于在尽可能多的地方有"const"注释的程序.其他人将程序定义为const-correct,当且仅当使用const注释时没有违反constness(即,它是编译器为您检查的属性).

所以我想知道,这些函数中哪些是正确的:

struct Person {
    string getName1() const { return _name; }
    string getName2() { return _name; }
    string getName3() const { _name = "John"; return _name; }
    string getName4() { _name = "John"; return _name; }

    string _name;
};
Run Code Online (Sandbox Code Playgroud)

我在互联网上搜索了定义,但实际上找不到明确的答案,我也怀疑可能存在一个发生过中的柑橘生成案例.那么任何人都可以为定义提供任何可靠的引用吗?

c++ const

5
推荐指数
1
解决办法
706
查看次数

标签 统计

c++ ×1

const ×1