C++ One Header Multiple Sources

前端 未结 4 897
时光说笑
时光说笑 2021-02-19 22:35

I have a large class Foo1:

class Foo {
public:
    void apples1();
    void apples2();
    void apples3();

    void oranges1();
    void         


        
相关标签:
4条回答
  • 2021-02-19 23:08

    From a technical standpoint, there is no penalty to doing this at all, but I have never seen it done in practice. This is simply a issue of style, and in that spirit, if it helps you to better read the class, then you would be doing yourself a disservice by not using multiple source files.

    edit: Adding to that though, are you physically scrolling through your source, like, with your middle mouse wheel? As someone else already mentioned, IDE's almost universally let you right click on a function declaration, and go to the definition. And even if that's not the case for your IDE, and you use notepad or something, it will at least have ctrl+f. I would be lost without find and replace.

    0 讨论(0)
  • 2021-02-19 23:09

    Are there any major design flaws to keeping the definition of the class in foo.h and splitting the implementation of the functions into foo_apples.cpp and foo_oranges.cpp.

    to pick nits: Are there any major design flaws to keeping the declaration of the class in foo.h and splitting the definitions of the methods into foo_apples.cpp and foo_oranges.cpp.

    1) apples and oranges may use the same private programs. an example of this would be implementation found in an anonymous namespace.

    in that case, one requirement would be to ensure your static data is not multiply defined. inline functions are not really a problem if they do not use static data (although their definitions may be multiply exported).

    to overcome those problems, you may then be inclined to utilise storage in the class -- which could introduce dependencies by increasing of data/types which would have otherwise been hidden. in either event, it can increase complexity or force you to write your program differently.

    2) it increases complexity of static initialization.

    3) it increases compile times

    the alternative i use (which btw many devs detest) in really large programs is to create a collection of exported local headers. these headers are visible only to the package/library. in your example, it can be illustrated by creating the following headers: Foo.static.exported.hpp (if needed) + Foo.private.exported.hpp (if needed) + Foo.apples.exported.hpp + Foo.oranges.exported.hpp.

    then you would write Foo.cpp like so:

    #include "DEPENDENCIES.hpp"
    #include "Foo.static.exported.hpp" /* if needed */
    #include "Foo.private.exported.hpp" /* if needed */
    #include "Foo.apples.exported.hpp"
    #include "Foo.oranges.exported.hpp"
    
    /* no definitions here */
    

    you can easily adjust how those files are divided based on your needs. if you write your programs using c++ conventions, there are rarely collisions across huge TUs. if you write like a C programmer (lots of globals, preprocessor abuse, low warning levels and free declarations), then this approach will expose a lot of issues you probably won't care to correct.

    0 讨论(0)
  • 2021-02-19 23:19

    Actually i don't see any reasons to split implementation because other developers should work with the interface, but not the implementation.

    Also any normal IDE provide an easy ability to jump from function declaration to it's defenition. So there is no reason to search the function implementations manually.

    0 讨论(0)
  • 2021-02-19 23:21

    Yes, you can define the class in one header file and split the function implementations accross multiple source files. It is not usually the common practice but yes it will work and there will be no overheads.

    If the aim to do so, is just plain readability, then perhaps it is not a good idea to do so, because it is not so common practice to have class function definitions accross multipls source files and might just confuse someone.

    0 讨论(0)
提交回复
热议问题