简体   繁体   English

在类定义中定义的成员函数是否与在C ++中其他地方定义的成员函数的编译方式不同?

[英]Are member functions defined in a class definition compiled differently than member functions defined elsewhere in C++?

Are member functions defined in a class definition compiled differently than member functions defined elsewhere in C++? 在类定义中定义的成员函数是否与在C ++中其他地方定义的成员函数的编译方式不同? For example, consider the following foo.h 例如,考虑以下foo.h

#pragma once
struct foo {
    void bar() {}
    void buz();
};

and foo.cpp 和foo.cpp

#include "foo.h"
void foo::buz() {};

If we look at the symbols for foo.o 如果我们看一下foo.o的符号

$ g++ -c foo.cpp

$ nm -a foo.o
0000000000000000 b .bss
0000000000000000 n .comment
0000000000000000 d .data
0000000000000000 r .eh_frame
0000000000000000 a foo.cpp
0000000000000000 n .note.GNU-stack
0000000000000000 t .text
0000000000000000 T _ZN3foo3buzEv

$ c++filt _ZN3foo3buzEv
foo::buz()

we see that we only have a symbol for foo::buz . 我们看到我们只有foo::buz的符号。 Now, say that we compile multiple files that all include foo.h and then create a library from the result. 现在,假设我们编译了多个包含foo.h文件,然后从结果中创建一个库。 Are the member functions bar and buz treated differently? 成员函数barbuz是否区别对待?

Yes, there is a difference. 是,有一点不同。 If a member function is defined inside class definition then the compiler tries to make it an inline function. 如果在类定义中定义了成员函数,则编译器会尝试使其成为内联函数。 For your example, the function is simple enough to make it inline. 对于您的示例,该函数非常简单,可以使其内联。 So the compiler has made bar inline and you only see symbol of baz . 所以编译baz bar内联,你只看到baz符号。

Whether it will be good or bad depends largely on the specific functions and your use case. 它的好坏在很大程度上取决于具体的功能和用例。 Inline function do not need an actual function call, the there is a performance improvement there. 内联函数不需要实际的函数调用,那里有性能改进。 But the downside is if you include the class header in many places then that will increase the binary size. 但缺点是如果在许多地方包含类标题,那么将增加二进制文件大小。

Also note that inline is a request to the compiler. 另请注意,inline是对编译器的请求。 The compiler is free to ignore the request and treat it a normal method. 编译器可以自由地忽略请求并将其视为常规方法。

As from 9.2.1/1 : 9.2.1 / 1开始

A member function may be defined in its class definition, in which case it is an inline member function 可以在其类定义中定义成员函数,在这种情况下,它是内联成员函数

On the other side, from 9.2.1/2 : 另一方面,从9.2.1 / 2

An inline member function (whether static or non-static) may also be defined outside of its class definition provided either its declaration in the class definition or its definition outside of the class definition declares the function as inline orconstexpr. 内联成员函数(无论是静态的还是非静态的)也可以在其类定义之外定义,前提是它在类定义中的声明或在类定义之外的定义将函数声明为内联orconstexpr。

The question was: Are member functions defined in a class definition compiled differently than member functions defined elsewhere in C++? 问题是: 类定义中定义的成员函数是否与C ++中其他地方定义的成员函数进行了不同的编译?

It mostly depends on how you define them, as you can deduce from the citations above. 它主要取决于您如何定义它们,因为您可以从上面的引文中推断出来。
In your example, they are actually different. 在您的示例中,它们实际上是不同的。

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

相关问题 在“类定义”中定义的C ++成员函数中隐式“内联” - Is “inline” implicit in C++ member functions defined in class definition 在类主体外部定义的C ++成员函数的编译 - Compilation of C++ member functions defined outside the class body C ++类成员函数的正向定义 - forward definition of c++ class member functions 在初始化时定义了不同行为的C ++成员函数 - C++ member functions with different behavior defined at initialization 隐式定义的成员函数的生成是否为 C++ 中类层次结构中的每个类独立处理? - Does generation of implicitly defined member functions get handled independently for every class in a class hierarchy in C++? 在 C++ 中,为什么必须在类外部定义类成员函数以便单独编译? - In C++, why must class member functions be defined outside class for separate compilation? 在C ++中如何继承父类的非成员函数,这只是在文件中定义的? - In C++ how to inherit a parent class' non-member functions, which is simply defined in the file? 如何为类定义中定义的模板成员函数使用显式模板实例化? - How can I use explicit template instantiation for template member functions defined within a class definition? C++ class 成员的用户定义比较器 - User defined comparator for member of C++ class 访问稍后在C ++中定义的类的成员 - Access the member of a class that defined later in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM