简单的C ++ getter / setters [英] Simple C++ getter/setters

查看:170
本文介绍了简单的C ++ getter / setters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我写的getter和setter为(注意:真正的类在getter / setter中做更多的事情):

Lately I'm writing my getter and setters as (note: real classes do more things in getter/setter):

struct A {
  const int& value() const { return value_; } // getter
        int& value()       { return value_; } // getter/setter
 private:
  int value_;
};

它允许我执行以下操作:

which allows me to do the following:

auto a = A{2}; // non-const object a

// create copies by "default" (value always returns a ref!):
int b = a.value();  // b = 2, is a copy of value :)
auto c = a.value(); // c = 2, is a copy of value :)

// create references explicitly:
auto& d = a.value();  // d is a ref to a.value_ :)
decltype(a.value()) e = a.value(); // e is a ref to a.value_ :)
a.value() = 3; // sets a.value_ = 3 :)

cout << b << " " << c << " " << d << " " << e << endl; // 2 2 3 3

const auto ca = A{1};
const auto& f = ca.value();  // f is a const ref to ca.value_ :)
auto& g = ca.value(); // no compiler error! :(
// g = 4; // compiler error :)
decltype(ca.value()) h = ca.value(); // h is a const ref to ca.value_ :)
//ca.value() = 2; // compiler error! :)

cout << f << " " << g << " " << h << endl; // 1 1 1

这种方法不允许我:


  • 验证setter的输入(这是一个很大的BUT),

  • 在const成员函数中返回值我想让编译器捕捉到const对象的赋值: ca.value()= 2 )。更新:请参阅下面的cluracan答案。

  • validate the input for the setter (which is a big BUT),
  • return by value in the const member function (because I want the compiler to catch assignment to const objects: ca.value() = 2). Update: see cluracan answer below.

但是,我仍然使用这个很多,因为

However, I'm still using this a lot because


  • 大多数时候我不需要它,

  • 这使我能够从我们的接口中分离出我的类的实现细节,这就是我想要的。

示例:

struct A {
  const int& value(const std::size_t i) const { return values_[i]; }
        int& value(const std::size_t i)       { return values_[i]; }
  private:
   std::vector<int> values_; 
   // Storing the values in a vector/list/etc is an implementation detail.
   // - I can validate the index, but not the value :(
   // - I can change the type of values, without affecting clients :)
 }; 

现在回答问题:


  • 这种方法还有其他缺点吗?

  • 为什么人们喜欢:

    • getter / setters方法具有不同的名称?

    • 将值作为参数传递?
      只是用于验证输入或有其他主要原因吗?

    推荐答案


    • 通常使用访问器/ mutator是一个设计的气味,你的类public接口是不完整的。通常来说,你想要一个有用的公共接口,提供有意义的功能,而不是简单的get / set(这只比一个或两个步骤比我们在结构和函数的C)。每次你想写一个mutator,并且很多时候你想写一个访问器,只是退后一步,问你自己我真的*需要这个吗?

    • 只有习惯用语的人可能不准备期望这样的功能,所以它会增加维护者的时间来梳理你的代码。

    • 同名方法几乎与公共成员相同:在这种情况下只使用公共成员。当方法做两个不同的事情,命名他们两个不同的东西。

    • 非const引用返回的mutator将允许各种各样的别名问题,给成员,依靠它以后存在。通过使用单独的设置器功能,您可以防止用户对您的私人数据进行别名。

      • Generally using accessors/mutators at all is a design smell that your class public interface is incomplete. Typically speaking you want a useful public interface that provides meaningful functionality rather than simply get/set (which is just one or two steps better than we were in C with structs and functions). Every time you want to write a mutator, and many times you want to write an accessor first just take a step back and ask yourself "do I *really* need this?".
      • Just idiom-wise people may not be prepared to expect such a function so it will increase a maintainer's time to grok your code.
      • The same-named methods are almost the same as the public member: just use a public member in that case. When the methods do two different things, name them two different things.
      • The "mutator" returning by non-const reference would allow for a wide variety of aliasing problems where someone stashes off an alias to the member, relying on it to exist later. By using a separate setter function you prevent people from aliasing to your private data.
      • 这篇关于简单的C ++ getter / setters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆