C ++头文件如何包含实现? [英] How can a C++ header file include implementation?

查看:211
本文介绍了C ++头文件如何包含实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,不是任何方式的C / C ++专家,但我认为头文件的要点是声明函数,然后C / CPP文件定义实现。

Ok, not a C/C++ expert by any means, but I thought the point of a header file was to declare the functions, then the C/CPP file was to define the implementation.

但是,今天查看一些C ++代码,我发现这是在一个类的头文件...

However, reviewing some C++ code tonight, I found this in a class's header file...

public:
    UInt32 GetNumberChannels() const { return _numberChannels; } // <-- Huh??

private:
    UInt32 _numberChannels;

那么为什么在头部有一个实现?它与 const 关键字有关吗?是否内联一个类方法?

So why is there an implementation in a header? Does it have to do with the const keyword? Does that inline a class method? What exactly is the benefit/point of doing it this way vs. defining the implementation in the CPP file?

我看到很多类似的答案,但讽刺的是,没有人解释这个 const 关键字,(虽然有人在这个问题的意见)。答案基本上与内联相同,第一个答案也解释了 const word将得到投票。

I see a lot of similar answers, but ironically not one of them explain the const keyword here, (although someone did in the comments to this question.) Since all the answers basically say the same thing about inlining, the first answer that also explains that const word will get the vote.

推荐答案


好吧,不是任何方式的C / C ++专家,但我认为头文件的要点是声明函数,然后C / CPP文件是要定义实现。

Ok, not a C/C++ expert by any means, but I thought the point of a header file was to declare the functions, then the C/CPP file was to define the implementation.

头文件的真正目的是在多个源文件之间共享代码。 通常用于将声明与实现分离以实现更好的代码管理,但这不是必需的。可以编写不依赖于头文件的代码,并且可以编写只由头文件组成的代码(STL和Boost库就是很好的例子)。请记住,当预处理器遇到 #include 语句时,会将语句替换为正在引用的文件的内容, 只显示已完成的预处理代码。

The true purpose of a header file is to share code amongst multiple source files. It is commonly used to separate declarations from implementations for better code management, but that is not a requirement. It is possible to write code that does not rely on header files, and it is possible to write code that is made up of just header files (the STL and Boost libraries are good examples of that). Remember, when the preprocessor encounters an #include statement, it replaces the statement with the contents of the file being referenced, then the compiler only sees the completed pre-processed code.

因此,例如,如果您有以下文件:

So, for example, if you have the following files:

Foo.h:

#ifndef FooH
#define FooH

class Foo
{
public:
    UInt32 GetNumberChannels() const;

private:
    UInt32 _numberChannels;
};

#endif

Foo.cpp:

#include "Foo.h"

UInt32 Foo::GetNumberChannels() const
{
    return _numberChannels;
}

Bar.cpp:

#include "Foo.h"

Foo f;
UInt32 chans = f.GetNumberChannels();

预处理器分别解析Foo.cpp和Bar.cpp,然后解析编译器的代码:

The preprocessor parses Foo.cpp and Bar.cpp separately and produces the following code that the compiler then parses:

Foo.cpp:

class Foo
{
public:
    UInt32 GetNumberChannels() const;

private:
    UInt32 _numberChannels;
};

UInt32 Foo::GetNumberChannels() const
{
    return _numberChannels;
}

Bar.cpp:

class Foo
{
public:
    UInt32 GetNumberChannels() const;

private:
    UInt32 _numberChannels;
};

Foo f;
UInt32 chans = f.GetNumberChannels();

Bar.cpp编译成Bar.obj并包含调用 Foo :: GetNumberChannels()。 Foo.cpp编译为Foo.obj并包含 Foo :: GetNumberChannels()的实际实现。编译之后,链接器会匹配.obj文件,并将它们链接在一起以生成最终的可执行文件。

Bar.cpp compiles into Bar.obj and contains a reference to call into Foo::GetNumberChannels(). Foo.cpp compiles into Foo.obj and contains the actual implementation of Foo::GetNumberChannels(). After compiling, the linker then matches up the .obj files and links them together to produce the final executable.


So why is there an implementation in a header?

通过在方法声明中包含方法实现,它被隐式声明为inline(有一个实际的 inline 关键字,也可以明确使用)。指示编译器应该内联函数只是一个提示,不保证函数实际上会被内联。但是如果是这样,那么无论从何处调用内联函数,函数的内容都直接复制到调用点,而不是生成 CALL 语句跳转到函数并退出时跳回到调用者。如果可能,编译器可以考虑周围的代码并进一步优化复制的代码。

By including the method implementation inside the method declaration, it is being implicitally declared as inlined (there is an actual inline keyword that can be explicitally used as well). Indicating that the compiler should inline a function is only a hint which does not guarantee that the function will actually get inlined. But if it does, then wherever the inlined function is called from, the contents of the function are copied directly into the call site, instead of generating a CALL statement to jump into the function and jump back to the caller upon exiting. The compiler can then take the surrounding code into account and optimize the copied code further, if possible.


关键字?

Does it have to do with the const keyword?

否。 const 关键字只是指示编译器该方法不会改变在运行时被调用的对象的状态。

No. The const keyword merely indicates to the compiler that the method will not alter the state of the object it is being called on at runtime.


与在CPP文件中定义实现相比,这样做的好处/意义是什么?

What exactly is the benefit/point of doing it this way vs. defining the implementation in the CPP file?

有效使用时,它允许编译器通常生成更快,更好的优化机器码。

When used effectively, it allows the compiler to usually produce faster and better optimized machine code.

这篇关于C ++头文件如何包含实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆