仅限标头的C ++应用程序有什么问题?

时间:2021-04-10 15:08:11

I'm wondering to code a C++ application with a header-only layout like the following:

我想知道编写一个C ++应用程序与只有标题的布局,如下所示:

// code3.h
#include <iostream>
class code3
{
public:
  void print()
  {
    std::cout << "hello " << std::endl;
  }
};

// code2.h
#include "code3.h"
class code2
{
public:
  void print()
  {
    code3 c;
    c.print();
  }
};

// code1.h
#include "code3.h"    
class code1
{
public:
  void print()
  {
    code3 c;
    c.print();
  }
};

// main.cpp
#include "code1.h"
#include "code2.h"

int main()
{
  code1 c1; 
  c1.print();

  code2 c2; 
  c2.print();
}

The only cpp file will be the main file. The rest of the code will be placed in header files.

唯一的cpp文件将是主文件。其余代码将放在头文件中。

I would like to know if there is some kind of performance problem with this approach. I know that defining the methods in the class declarations inlines them, but as it will be only one cpp file, the inline methods won't be duplicated. I just want to focus my question on the performance. I'm not talking about extensibility, legibility, maintenance or anything else. I want to know if I'm missing something with this approach that could generate a performance problem.

我想知道这种方法是否存在某种性能问题。我知道在类声明中定义方法会将它们内联,但由于它只是一个cpp文件,因此内联方法不会重复。我只想把我的问题集中在表现上。我不是在谈论可扩展性,易读性,维护或其他任何东西。我想知道我是否遗漏了这种可能会产生性能问题的方法。

Thanks!

2 个解决方案

#1


3  

You will find that this becomes rather unpractical when your project has several hundred files (or more), and ALL of the code has to be recompiled EVERY time you change something.

当你的项目有几百个文件(或更多)时,你会发现这变得相当不实用,并且每当你改变一些东西时都必须重新编译所有的代码。

In a small software project, there's little reason to have different source files, but there is no huge drawback of having more than one source file.

在一个小型软件项目中,几乎没有理由拥有不同的源文件,但是拥有多个源文件并没有什么大的缺点。

When the source starts to be more than a dozen files, compile time starts to increase. It's also much harder to isolate functional groups of code, which in turn affects the ease with which you can take one lump of code and use it in a different project - which is often a useful thing when working on code.

当源开始超过十几个文件时,编译时间开始增加。隔离代码的功能组也变得更加困难,这反过来又影响了你可以轻松地获取一块代码并在不同的项目中使用它 - 这在处理代码时通常是有用的。

#2


1  

Last time I asked this question, I got a TON of useful answers: http://www.daniweb.com/software-development/cpp/threads/423106/separate-headers-from-source

上次我问这个问题时,我得到了一些有用的答案:http://www.daniweb.com/software-development/cpp/threads/423106/separate-headers-from-source

Basically I asked why I should separate my source from headers because I also used to hate having "extra" files and switching back and forth between the header and the source. I think the answers I got may be useful for you so I'm just going to leave that link above.

基本上我问为什么我应该将我的源与标题分开,因为我还习惯于讨厌“额外”文件并在标题和源之间来回切换。我认为我得到的答案可能对你有用,所以我只想在上面留下这个链接。

#1


3  

You will find that this becomes rather unpractical when your project has several hundred files (or more), and ALL of the code has to be recompiled EVERY time you change something.

当你的项目有几百个文件(或更多)时,你会发现这变得相当不实用,并且每当你改变一些东西时都必须重新编译所有的代码。

In a small software project, there's little reason to have different source files, but there is no huge drawback of having more than one source file.

在一个小型软件项目中,几乎没有理由拥有不同的源文件,但是拥有多个源文件并没有什么大的缺点。

When the source starts to be more than a dozen files, compile time starts to increase. It's also much harder to isolate functional groups of code, which in turn affects the ease with which you can take one lump of code and use it in a different project - which is often a useful thing when working on code.

当源开始超过十几个文件时,编译时间开始增加。隔离代码的功能组也变得更加困难,这反过来又影响了你可以轻松地获取一块代码并在不同的项目中使用它 - 这在处理代码时通常是有用的。

#2


1  

Last time I asked this question, I got a TON of useful answers: http://www.daniweb.com/software-development/cpp/threads/423106/separate-headers-from-source

上次我问这个问题时,我得到了一些有用的答案:http://www.daniweb.com/software-development/cpp/threads/423106/separate-headers-from-source

Basically I asked why I should separate my source from headers because I also used to hate having "extra" files and switching back and forth between the header and the source. I think the answers I got may be useful for you so I'm just going to leave that link above.

基本上我问为什么我应该将我的源与标题分开,因为我还习惯于讨厌“额外”文件并在标题和源之间来回切换。我认为我得到的答案可能对你有用,所以我只想在上面留下这个链接。