编写成员函数时更好的做法是什么?

发布于 2024-07-07 17:48:32 字数 91 浏览 7 评论 0原文

我见过成员函数既可以在它们所属的类内部编程,也可以在类外部使用类内部的函数原型进行编程。 我只使用第一种方法进行编程,但想知道使用其他方法还是仅使用个人喜好是否更好?

I have seen member functions programed both inside of the class they belong to and outside of the class with a function prototype inside of the class. I have only ever programmed using the first method, but was wondering if it is better practice to use the other or just personal preference?

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

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

发布评论

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

评论(8

唔猫 2024-07-14 17:48:33

对于 C++,将方法定义放入头文件中意味着当头发生更改时,包含给定头的所有内容都必须重新编译 - 即使它只是实现细节。

将定义移出标头意味着仅当标头本身发生更改(添加/删除函数或声明更改)时,才需要重新编译包含标头的文件。 这会对复杂项目的编译时间产生很大影响。

For C++, putting method definitions in the header file means that everything that includes a given header must be recompiled when the header changes - even if it's just an implementation detail.

Moving definitions out of the header means that files which include the header will need to be recompiled only when the header itself changes (functions added/removed, or declarations changed). This can have a big impact on compile times for complex projects.

变身佩奇 2024-07-14 17:48:33

这两种技术各有优点。

如果您仅将原型放在类定义中,那么使用您的类的人就可以更轻松地查看哪些方法可用。 他们不会因实施细节而分心。

将代码直接放在类定义中可以使类的使用更加简单,只需#include 标头即可。 这对于模板化类特别有用(必要)。

There's advantages to both techniques.

If you place only prototypes in the class definition, that makes it easier for someone who is using your class to see what methods are available. They aren't distracted by implementation details.

Putting the code directly in the class definition makes it simpler to use the class, you only have to #include a header. This is especially useful (necessary) with templated classes.

时光是把杀猪刀 2024-07-14 17:48:33

假设语言是 C++:

底线是个人喜好。 类里面整体更短,更直接,特别是对于

int getFoo() const { return _foo; }

函数类型。
在类之外,可以从类定义中删除“混乱”。

我见过两者都在使用......

当然,非内联函数总是在类之外。

Presuming the language is C++:

The bottom line is that is personal preference. Inside the class is shorter overall and more direct, especially for the

int getFoo() const { return _foo; }

type of function.
Outside te class, can remove "clutter" from the class definition.

I have seen both in use...

Of course, non-inlined functions are always outside the class.

不爱素颜 2024-07-14 17:48:33

在定义类时混合使用两种样式也很常见。 对于由 1 或 2 行组成的简单方法,在类定义中定义方法主体是常见且方便的。 对于更冗长的方法,最好在外部定义它们。 您将获得更具可读性的类定义,而不会使其与方法主体混淆。

隐藏方法的实现是有益的,因为类的用户不会因实际实现而分心,或者对以后可能更改的实现做出假设。

It is also common to mix both styles when defining a class. For simple methods consisting of 1 or 2 lines it is common and convenient to define the method body within the class definition. For more lengthy methods it is better to define these externally. You will have more readable class definitions without cluttering them up with the method body.

Hiding the implementation of a method is beneficial in that the user of the class will not be distracted by the actual implementation, or make assumptions about the implementation that might change at a later time.

撞了怀 2024-07-14 17:48:33

我假设你在谈论 C++。

拥有一个漂亮干净的界面当然是一个好主意。 拥有单独的实现文件有助于保持界面整洁。

它还可以减少编译时间,特别是当您使用不透明指针时。

I assume you are talking about C++.

Having a nice and clean interface is certainly a good idea. Having a separate implementation file helps to keep your interface clean.

It also reduces compilation time, especially if you are using an opaque pointer.

呆° 2024-07-14 17:48:33

如果在类内部实现该函数,则不能在多个 .cpp 文件中 #include 该类,否则链接器会抱怨该函数的多个定义。

因此,通常的做法是将类定义放在 .h 文件中,并将成员实现放在 .cpp 文件中(通常具有相同的名称)。

If you implement the function inside the class, you cannot #include the class in multiple .cpp files or the linker will complain about multiple definitions of the function.

Thus, usual practice is to have the class definition in a .h file and the members implementation in a .cpp file (usually with the same name).

独自唱情﹋歌 2024-07-14 17:48:33

再次,在模拟 C++ 时,我通常将其限制为虚拟函数上的占位符,例如

virtual int MyFunc() {}  // Does nothing in base class, override if needed

任何其他内容,而 Andrew Medico 的观点太容易出现并且会损害编译时间。

Again, assiming C++, I usually restrict this to placeholders on virtual functions, e.g.

virtual int MyFunc() {}  // Does nothing in base class, override if needed

Anything else, and Andrew Medico's point kicks in too easily and hurts compile times.

薄荷梦 2024-07-14 17:48:32

假设您指的是 C++,最好在类外部定义函数,因为如果将其放在类内部,编译器可能会尝试内联它,这并不总是可取的:

  1. 增加代码大小(包含此标头的每个目标文件最终可能会在其代码中包含该函数的副本)。
  2. 函数定义更改时会破坏二进制兼容性。

即使使用内联函数,通常最好将定义放在类外部以提高类公共接口的可读性,除非该函数是一个简单的访问器或其他一些单行函数。

Assuming you mean C++, it is always better to define functions outside of the class, because if you put it inside the class, compiler may try to inline it, which is not always desirable:

  1. Increase in code size (every object file that includes this header might end up with a copy of the function in their code).
  2. Breaking binary compatibility when function definition changes.

Even with inline functions, it is usually better to put definitions outside the class to improve readability of class public interface, unless the function is a trivial accessor or some other one-liner.

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