简体   繁体   English

将所有方法放在类定义中

[英]Putting all methods in class definition

When I use the pimpl idiom, is it a good idea to put all the methods definitions inside the class definition? 当我使用pimpl习语时,将所有方法定义放在类定义中是否是个好主意? For example: 例如:

// in A.h

class A {
   class impl;
   boost::scoped_ptr<impl> pimpl;
public:
   A();
   int foo();
}

// in A.cpp

class A::impl {
   // method defined in class
   int foo() {
       return 42;
   }

   // as opposed to only declaring the method, and defining elsewhere:
   float bar();
};

A::A() : pimpl(new impl) { }
int A::foo() {
   return pimpl->foo();
}

As far as I know, the only problems with putting a method definition inside a class definition is that (1) the implementation is visible in files that include the class definition, and (2) the compiler may make the method inline. 据我所知,将方法定义放在类定义中的唯一问题是(1)实现在包含类定义的文件中可见,(2)编译器可以使方法内联。

These are not problems in this case since the class is defined in a private file, and inlining has no effect since the methods are called in only one place. 在这种情况下,这些不是问题,因为类是在私有文件中定义的,并且内联没有任何效果,因为只在一个地方调用方法。

The advantage of putting the definition inside the class is that you don't have to repeat the method signature. 将定义放在类中的优点是您不必重复方法签名。

So, is this OK? 那么,这样可以吗? Are there any other issues to be aware of? 还有其他问题需要注意吗?

I think you answered your own question : both solutions are equivalent. 我想你回答了自己的问题:两种解决方案都是等价的。

However, I wouldn't be so sure that 'inlining has no effect since the methods are called in only one place' : an additional call could exists when the functions are not inlined. 但是,我不太确定'内联没有任何影响,因为只在一个地方调用方法':当函数没有内联时, 可能存在额外的调用。 But chances are that the compiler is smart enough to optimize them away from the one-line forwarding calls in the outer class. 但是很有可能编译器足够智能,可以远离外层的单行转发调用。

In the end, I believe it's just a matter of taste. 最后,我认为这只是一个品味问题。

Advantages: 好处:

  • all code of the class is localized 该类的所有代码都已本地化

Disadvantages: 缺点:

  • for larger classes: when scrolling is needed, it becomes more difficult to know to which class the function belongs. 对于较大的类:当需要滚动时,更难以知道函数属于哪个类。
  • dependencies are more easily solved when functions reside after all class declarations. 当函数驻留在所有类声明之后时,依赖关系更容易解决。 Otherwise, it might be needed that some class declarations are moved after others and some functions still have to be moved after the class declaration when there are mutual dependency of internal classes. 否则,可能需要将某些类声明移到其他类之后,并且在内部类相互依赖时,仍然必须在类声明之后移动某些函数。

Usually I don't add methods to the Impl inner class, but I can't see any issue if you define the methods inline. 通常我不会向Impl内部类添加方法,但如果您定义内联方法,我看不出任何问题。 It seems to me much more readable than having seperate declaration and definition. 在我看来,比单独的声明和定义更具可读性。

Whether the compiler inlines the methods depends on the compiler and the passed parameters. 编译器是否内联方法取决于编译器和传递的参数。

In the case of the pimpl idiom, I don't think it matters whether the methods are defined within the Imp's body or not. 在pimpl成语的情况下,我认为这些方法是否在Imp的体内定义并不重要。 I personally like them defined outside, because it is easy to see what is really important (like member variables and list of methods). 我个人喜欢在外面定义它们,因为很容易看出什么是真正重要的(比如成员变量和方法列表)。

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

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