成员函数定义

发布于 2024-10-15 01:44:25 字数 108 浏览 4 评论 0原文

正确的方法是什么:

在类内部定义成员(类)函数?

在类外部定义成员(类)函数?

谢谢。

What is the right approach to take:

Define the member (class) function inside the class?

Define the member (class) function outside the class?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萧瑟寒风 2024-10-22 01:44:25

假设您正在谈论这三种可能性:

  1. 头文件中的类定义中定义的方法。
  2. 方法在头文件中的类定义之外定义。
  3. 方法定义在实现文件中的类定义之外。

那么项目和公司指南可能会迫使您始终使用(1)或(3)。

当您有选择时,恕我直言,最好适应当前的情况,考虑诸如“

  • 您想要一个仅标头模块吗?” 之类的事情。那么(1)默认,(2)可能。
  • 手段是大猛兽吗?然后(2)或(3)。
  • 模板方法专业化?然后(2)或(3)。
  • 存在构建时问题(构建缓慢)?表示(3)。
  • 模板类? (1) 或可能 (2)

但除非您实际上被迫做出选择,否则首先要考虑代码的清晰度

干杯&呵呵,

Assuming you're talking about these three possibilities:

  1. Method defined in class definition in header file.
  2. Method define outside class definition in header file.
  3. Method define outside class definition in implementation file.

Then project and company guidelines may force you to use (1) or (3) always.

When you have a choice, it's IMHO best to adapt to circumstances at hand, considering things such as

  • Do you want a header-only module? Then (1) as default, (2) possible.
  • Is the method a large beast? Then (2) or (3).
  • Template method specialization? Then (2) or (3).
  • There is a build-time problem (slow builds)? Indicates (3).
  • Template class? (1) or possibly (2)

But except where the choice is effectively forced on you, above all consider the clarity of your code.

Cheers & hth.,

浮云落日 2024-10-22 01:44:25

一个常见的建议是使标题尽可能简单和干净。标头将包含在外部代码中,并且它们必须处理您在那里编写的所有内容。如果您在标头中编写一个方法,则所有翻译单元都会编译该函数,以便链接器稍后可以丢弃除其中一个之外的所有函数。

如果您的代码对不属于接口一部分的类型或库有内部依赖性,则通过在类声明中内联成员函数的代码,该类的定义或该库的标头将必须包含在您的标头,这意味着您正在向用户泄露您的实现细节。

A common advice is to keep headers as simple and clean as possible. Headers will be included by external code, and they will have to process everything that you have written there. If you write a method in the header, all translation units will compile that function, only so that the linker can discard all but one of them later on.

If your code has an internal dependency on a type or library that is not part of your interface, then by inlining the code of the member function in the class declaration the definition of that class or the headers of that library will have to be included in your header, and that means that you are leaking your implementation details to your users.

ま柒月 2024-10-22 01:44:25

除非成员函数定义很简单(非正式意义上)并且不会引入任何额外的依赖项,否则我通常会在类主体之外的单独源文件中定义成员函数。

这通常是风格问题,但在某些情况下这是必要的,而在许多其他情况下需要在类体之外定义函数。

例如,如果您有相互依赖的类,并且在类定义之前只能使用另一个类的前向声明,则使用该另一个类的定义的成员函数只能在类体外部定义已提供另一个类的完整定义。

Unless the member function definition is trivial (in an informal sense) and doesn't introduce any additional dependencies I would normally define a member function outside of the class body in a separate source file.

It's often a matter of style but there are some cases in which it is necessary and many other cases in which it is desirable to define function outside of the class body.

For example, in the cases where you have interdependent classes and only a forward declaration of another class can be made available before the class definition, a member function which uses the definition of that other class can only be defined outside of the class body after a full definition of the other class has been provided.

携余温的黄昏 2024-10-22 01:44:25

您的意思是“在类声明/ .h 文件中”还是“在 .cpp 文件中使用 ::”?

如果是这样,我总是选择后者。当涉及到调试时,单步执行并查看发生了什么要容易得多。它还有助于整理类声明,不需要知道任何实现细节”

Do you mean "in the class declaration / .h file" vs "in a .cpp file using ::" ?

If so I always go for the latter. When it comes to debugging, it's a lot easier to step through and see what's going on. It also helps declutter the class declaration, which doesn't need to know any implementation details"

眼趣 2024-10-22 01:44:25

如果您想在类内部定义函数,最基本的语法通常如下所示:

class Object
{
  int property;
  void doSomething()
  {
      property=100;
  }
};

如果您想在外部定义函数,则类似于在 main 和 in 之前声明函数库文件。在你的类中,你有:

class Object
{
  int property;
  void doSomething();
};

然后在你的类之后的某个地方,在 main() 函数之后或在包含的文件中,你可以有定义:

void Object::doSomething()
{
  property=100;
}

有些将类放在头文件中,并将定义放在该头使用的 cpp 文件中。可以采用各种技术。

这两种方法都是有效的。通常,我会直接在类中包含非常小的和/或核心类功能,以及我倾向于将其进行较繁重的批量工作的其他功能分开。试着想想遇到你的代码并想要改变它的区别。

If you want to define a function within a class the most basic syntax looks generally like:

class Object
{
  int property;
  void doSomething()
  {
      property=100;
  }
};

If you want to define a function outside it is similar to declaring functions before main and in library files. In your class you have:

class Object
{
  int property;
  void doSomething();
};

Then somewhere after your class, after the main() function or in an included file you can have the definition:

void Object::doSomething()
{
  property=100;
}

Some place classes in a header file and the definitions in a cpp file used by that header. Various techniques possible.

Both of these approaches are valid. Often I will include very small and/or core class functionality directly within the class and other functions which do heavier bulk work I tend to separate. Try to think the difference in coming upon your code and wanting to alter it.

失去的东西太少 2024-10-22 01:44:25

如果我们根据性能问题来看,那么在类中声明函数是更有效的方法。因为在编译时,它连接了所有函数调用和其他组件,因此将所有内容都放在一个源中会很容易并且必须更快......

if we see according to performance issue than it is more effective way to declare the function in the class . becouse at the compile time it conects all the funcation calls and other components so it will easy and must be faster to get all in one source...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文