简体   繁体   English

你应该在成员函数中传递成员变量吗?

[英]Should you pass member variables within member functions?

Sort of a style question here. 这里有一个风格问题。 Say I have a class A which has to do a sequence of reasonably complex things to its member variable B b 假设我有一个AA它必须对其成员变量B b做一系列相当复杂的事情

class A {
 public:
  void DoStuffOnB(){
   DoThing1();
   DoThing2();
   DoThing3();
  }
 private:
  B b;
  void DoThing1(){ /* modify b */ }
  void DoThing2(){ /* modify b */ }
  void DoThing3(){ /* modify b */ }
};

where the DoThings functions only depend on b (or other member variables and some passed parameters). 其中DoThings函数仅依赖于b (或其他成员变量和一些传递的参数)。 If I want to make those functions re-usable in the future outside of that class, I'm better off writing them as: 如果我想让这些函数在将来可以在该类之外重用,我最好把它们写成:

class A {
 public:
  void DoStuffOnB(){
   DoThing1(b);
   DoThing2(b);
   DoThing3(b);
  }
 private:
  B b;
  void DoThing1(B& b){ /* modify b */ }
  void DoThing2(B& b){ /* modify b */ }
  void DoThing3(B& b){ /* modify b */ }
};

and then my DoThing functions can just be copied elsewhere in the future. 然后我的DoThing功能可以在以后的其他地方复制。 Am I better off writing the function to take all relevant parameters like that, or should the function only take non-member parameters? 我最好编写函数来获取所有相关参数,或者该函数是否只采用非成员参数?

In case the answer is "you should write the function to take all relevant parameters", why would one bother to put it in a class? 如果答案是“你应该编写函数来获取所有相关参数”,为什么还要把它放在一个类中呢?

When should you use a free function, and when should you use a member function? 什么时候应该使用免费功能,何时使用会员功能?

Assuming from the context that the "do something on B" functions only operate on the B member and not other state in A then: 假设从上下文中“在B上执行某些操作”功能仅对 B成员进行操作,而不是A其他状态:

  • If the functions directly manipulate/operate on the private state of B then they should be members of B . 如果函数直接操作/操作B的私有状态,那么它们应该是B成员。
  • Else they should be free functions. 否则他们应该是自由职能。

A member function is a member function because its' scope has access to the member variables without having to use referencing and pointer syntax. 成员函数是成员函数,因为它的“范围”可以访问成员变量,而无需使用引用和指针语法。 As someone mentioned earlier this would most likely make it simpler to code and maintain so you would use this method unless you needed the function to be a free function that might take the same type data but from different classes in which case you would have to pass by reference or use pointers to gain access to the scope of the variable. 正如前面提到的那样,这很可能会使代码和维护变得更简单,所以你会使用这个方法,除非你需要这个函数是一个自由函数,它可能采用相同类型的数据但是来自不同的类,在这种情况下你必须通过通过引用或使用指针来获取对变量范围的访问。

Should you pass member variables within member functions? 你应该在成员函数中传递成员变量吗?

There is no need to pass member variables to member functions, since the member functions have access to all the data members. 不需要将成员变量传递给成员函数,因为成员函数可以访问所有数据成员。

It's similar to free standing functions accessing static file local variables. 它类似于访问静态文件局部变量的自由站立函数。 The functions have access to the statically declared variables in the same translation unit. 这些函数可以访问同一翻译单元中的静态声明变量。

When should you use a freestanding function and when should you use a member function? 什么时候应该使用独立功能?什么时候应该使用会员功能?

In general, use a member function when the functionality is associated with the object. 通常,在功能与对象关联时使用成员函数。

Use a freestanding function when 使用独立功能时

  • the class has static members 该类有静态成员

  • or functionality is associated with a class and doesn't use static members. 或功能与类关联,不使用静态成员。

You can also use freestanding functions when the same functionality can apply to different objects. 当相同的功能可以应用于不同的对象时,您还可以使用独立功能。
For example, let's talk serialization or outputting of an object. 例如,让我们谈谈对象的序列化或输出。

One can define a method, load_from_buffer() in an object, but it won't work with POD types. 可以在对象中定义方法load_from_buffer() ,但它不适用于POD类型。

However, if a function load_from_buffer() is made freestanding, it can be overloaded for different types, such as int , char , double and with templates, an overload can be made to call objects derived from an interface. 但是,如果函数load_from_buffer()是独立的,它可以为不同的类型重载,例如intchardouble和模板,可以重载调用从接口派生的对象。

Summary 摘要
Prefer to use member methods when they require access to data members of an object. 当需要访问对象的数据成员时,更喜欢使用成员方法。 Use static member methods when they access static data members or there is a need for the functionality without an instance of an object (think encapsulation). 在访问静态数据成员时使用静态成员方法,或者需要没有对象实例的功能(想想封装)。 Freestanding functions also provide the capability of functionality to different objects based on function overloading. 独立功能还基于功能重载为不同对象提供功能。

There are no hard rules, just use what you think will be easiest to maintain, assist in correctness and robustness and speed up development. 没有硬规则,只需使用您认为最容易维护的内容,协助正确性和稳健性并加快开发。

Just to confuse people, here is an article by Scott Meyers: 只是为了让人迷惑,这是Scott Meyers的一篇文章:
How Non-Member functions increase encapsulation 非成员函数如何增加封装

Remember, in order for a free standing function to access data members of an object, the data members must be given public access or the function needs to be a friend of the object. 请记住,为了使自由站立功能访问对象的数据成员,必须为数据成员提供公共访问权限,或者该功能需要是该对象的朋友。 The classic example is overloading the stream operators for a class. 典型的例子是重载类的流操作符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM