如果我在头文件中实现一个类会发生什么? [英] What happens if I implement a class in the header file?

查看:226
本文介绍了如果我在头文件中实现一个类会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++中的内联函数

在其头文件中实现一个类?一个典型的例子如下:

What does the compiler do if I completely implement a class in its header file? A typical example follows:

class MyException
{    
public:
    explicit MyException(const char* file, int line) file(file), line(line) {};
    const char* getFile() const { return file };
    int getLine() const { return line };
private:
    const char* const file;
    const int line;
};

我打算使用这样的类: throw MyException(__ FILE__, __LINE __)

My intention is to use the class like this: throw MyException(__FILE__, __LINE__).

我将此头文件包含到每个.cpp文件中。我认为编译器将编译类多次,因为它定义和包括(相同)机器代码到它产生的每个目标文件。现在,链接器会做什么?我尝试了一个更简单的例子(没有所有那些讨厌的 const '),它编译得很好。

I include this header file into each .cpp file. I suppose the compiler will compile the class as many times as it is defined and include the (identical) machine code into every object file it produces. Now, what will the linker do? I tried a simpler example (without all those pesky const's) and it compiled fine.

,如果不是一个简单的类,我在头文件中实现了一个三屏长的C函数?最后一个问题,我应该把我的例子分成.h和.cpp文件吗?

What would happen, if instead of a simple class, I implemented a three-screenful-long C function in a header file? And the final question, should I split my example into .h and .cpp files?

推荐答案

产生任何代码。它只是显示类的用户如何布局,所以他们可以生成适当的代码来操作它。

A class definition itself doesn't produce any code. It just shows users of the class how it is layed out, so they can generate appropriate code to manipulate it.

这是类的成员函数生成代码。当您在类定义中定义一个成员函数时,它会为函数提供一个隐式的 inline 声明。

It's the member functions of the class that generate code. When you define a member function inside the class definition it gives the function an implicit inline declaration.

可以通过以下两种方式之一进行编译和链接:

A function call can be compiled and linked in one of two ways:

(1)可以将一个带有RETURN汇编指令的函数代码复制到图像中,并且可以在调用位置放置CALL装配指令(连同参数传递和返回值传输)以将控制转移到该代码。

(1) A single copy of the function code with a RETURN assembly instruction at the end can be placed in the image, and a CALL assembly instruction can be placed (along with param passing and return value transfer) at the call site to transfer control to this code.

(2)函数实现的完整副本可以替换调用点的整个函数调用。

(2) An entire copy of the function implementation can replace the entire function call at the call site.

inline 是建议编译器以第二种方式做。此外, inline 声明允许在多个转换单元中定义函数(因此它可以放置在共享头文件中)。为了使编译器可以选择实现第二个方法,它需要在编译时的函数实现的副本。如果函数实现在外部翻译单元中,则不可用。

A function declared inline is a recommendation to the compiler to do it the second way. Further an inline declaration allows the function to be defined in several translation units (so it can be placed in a shared header file). In order for the compiler have the option of implementing the second method, it needs a copy of the function implementation at compile-time. This isn't available if the function implementation is in a foreign translation unit.

还应注意,现代编译器使用函数声明为内联来完成复杂的事情。请参阅:

It should also be noted that modern compilers do complicated things with functions declared inline. See:

http:// gcc.gnu.org/onlinedocs/gcc/Inline.html

这篇关于如果我在头文件中实现一个类会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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