Can I write C++ code without headers (repetitive function declarations)?

前端 未结 24 713
半阙折子戏
半阙折子戏 2020-11-28 07:55

Is there any way to not have to write function declarations twice (headers) and still retain the same scalability in compiling, clarity in debugging, and flexibility in desi

相关标签:
24条回答
  • 2020-11-28 08:14

    There is no practical way to get around headers. The only thing you could do is to put all code into one big c++ file. That will end up in an umaintainable mess, so please don't do it.

    At the moment C++ header-files are a nessesary evil. I don't like them, but there is no way around them. I'd love to see some improvements and fresh ideas on the problem though.

    Btw - once you've got used to it it's not that bad anymore.. C++ (and any other language as well) has more anoying things.

    0 讨论(0)
  • 2020-11-28 08:14

    After reading all the other answers, I find it missing that there is ongoing work to add support for modules in the C++ standard. It will not make it to C++0x, but the intention is that it will be tackled in a later Technical Review (rather than waiting for a new standard, that will take ages).

    The proposal that was being discussed is N2073.

    The bad part of it is that you will not get that, not even with the newest c++0x compilers. You will have to wait. In the mean time, you will have to compromise between the uniqueness of definitions in header-only libraries and the cost of compilation.

    0 讨论(0)
  • 2020-11-28 08:15

    What I have seen some people like you do is write everything in the headers. That gives your desired property of only having to write the method profiles once.

    Personally I think there are very good reasons why it is better to separate declaration and definition, but if this distresses you there is a way to do what you want.

    0 讨论(0)
  • 2020-11-28 08:21

    As far as I know, no. Headers are an inherent part of C++ as a language. Don't forget that forward declaration allows the compiler to merely include a function pointer to a compiled object/function without having to include the whole function (which you can get around by declaring a function inline (if the compiler feels like it).

    If you really, really, really hate making headers, write a perl-script to autogenerate them, instead. I'm not sure I'd recommend it though.

    0 讨论(0)
  • 2020-11-28 08:22

    It is best practice to use the header files, and after a while it will grow into you. I agree that having only one file is easier, but It also can leed to bad codeing.

    some of these things, althoug feel awkward, allow you to get more then meets the eye.

    as an example think about pointers, passing parameters by value/by reference... etc.

    for me the header files allow-me to keep my projects properly structured

    0 讨论(0)
  • 2020-11-28 08:23

    Learn to recognize that header files are a good thing. They separate how codes appears to another user from the implementation of how it actually performs its operations.

    When I use someone's code I do now want to have to wade through all of the implementation to see what the methods are on a class. I care about what the code does, not how it does it.

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