C++ const 访问器和引用最佳实践

标签 c++ reference constants accessor

在尝试重温我的 C++ 时,我一直在尝试找出创建访问器的最佳实践方法。

我想澄清我的理解,看看我的做法是否正确。我有几个问题,但它们看起来很简单,所以我将它们全部汇总到这个 Stack Overflow 问题中。

以下是一些示例代码,代表我“认为”是正确的做事方式:

class MyClass
{
private:
    std::string StringMember_;
    int IntMember_;

public:
    MyClass(const std::string &stringInput, const int &intInput) : StringMember_(stringInput), IntMember_(intInput)
    {
    }

    const std::string &StringMember() const
    {
        return StringMember_;
    }

    void StringMember(const std::string &stringInput)
    {
        StringMember_ = stringInput;
    }

    const int &IntMember() const
    {
        return IntMember_;
    }

    void IntMember(const int &intInput)
    {
        IntMember_ = intInput;
    }
};

我的问题是:

我的访问器返回一个const 引用变量,即const std::string,这意味着它(我的类的成员变量)不能被改变。对吗?

方法主体之前的最后一个 const 表示该方法所属的类的任何成员都不能更改,除非它们被指定为 mutable。这也是正确的吗?

我在 const 方法参数中传递的地方,这意味着我确保这些参数始终在传递时存储,从而保护传入的任何原始变量不被方法更改 body 。这也是正确的吗?

关于 mutable 关键字,在什么情况下我真的想使用它?我一直在努力想出一个好的场景,我有一个需要修改类成员的 const 方法。

访问器似乎是个好主意,即使对于永远不会公开的数据也是如此,因为它确保了单点入口,允许更容易调试等等。我的思路是正确的,还是这实际上完全没有意义,不需要私有(private)访问器?

从纯粹的句法角度来看,我应该像 const int& intInput 还是 const int &intInput 这样编写我的引用。 “&”号的位置真的很重要,还是只是个人喜好问题?

最后,我在上面的示例中所做的是好的做法吗?我计划开始从事一个更大的个人项目,我想在以后遇到问题之前先掌握这些核心基础知识。

我用这个作为引用:https://isocpp.org/wiki/faq/const-correctness

最佳答案

Where my accessors return a const reference variable, ie const std::string, this means that it (my class's member variable) cannot be changed. Is that correct?

正确。无法通过 const 引用更改变量。

The last const before a method's body indicates that no members of the class for which that method is a part of can be altered, unless they are designated mutable. Is this also correct?

正确。它还允许在 const 对象上调用该函数。

Where I'm passing in const method parameters, this means that I ensure these parameters are always stored as they were passed in, thus protecting any original variables being passed in from being altered by the method body. Is this also correct?

正确。通过按值接受参数也可以实现同样的效果。

With regards to the mutable keyword, under what circumstances would I actually want to use this?

参见 When have you used C++ 'mutable' keyword?

Accessors seem like a good idea, even for data that will never be publicly exposed, because it ensures a single-point of entry, allowing for easier debugging and so on. Am I thinking along the right lines here

我不赞成这种说法。观察点允许轻松调试成员变量,而不管它们是从哪里访问的。

From a purely syntactical perspective, should I be writing my references like const int& intInput or const int &intInput.

两者在句法上是等价的,它们之间的选择纯粹是美学上的。

Finally, is what I'm doing in the example above good practice?

没有统一的答案。访问器有时很有用。通常它们是多余的。如果您提供了一个允许直接设置值的函数,就像您在此处所做的那样,那么您还不如摆脱访问器并将成员公开。

关于C++ const 访问器和引用最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392919/

相关文章:

PHP 字符串常量过度使用?

python - 使用 Python Enum 或 Dictionary 映射常量并通过推理保持 DRY 的最佳方法

c++ - const-correctness 是否给编译器更多的优化空间?

java - 如何裁剪数组的一部分?

c++ - 按值返回的内存管理差异

c# - 在 C# 应用程序中运行 C++ exe 问题

c++ - 临时对象被销毁后为什么不崩溃

java - 如何在一个方法中使用另一个方法?

c++ - 制作带参数的命令行程序

c++ - 用于将容器累积到字符串中的标准算法,其中分隔符分隔条目?