C++ programming style

前端 未结 16 1691
眼角桃花
眼角桃花 2021-02-07 22:33

I\'m an old (but not too old) Java programmer, that decided to learn C++. But I have seen that much of C++ programming style, is... well, just damn ugly!

All that stuff

相关标签:
16条回答
  • 2021-02-07 22:50

    I worry from the tone of your question that you may be reading some bad C++ code in learning C++. Well-written code is usually not ugly in any language. As a starting point, you might try the online C++ FAQ, especially the chapter on learning C++.

    0 讨论(0)
  • 2021-02-07 22:51

    .h and .cpp files often separate declarations from definitions, and there is some kind of order here, and conceptual encapsulation. headers have the 'what', implementation files have the 'how'.

    I find having everything in one file in java to be disorganized. So, each to their own.

    C++ conventions and idioms all have well thought out reasoning, just like any other language does for the idioms its community adopts.

    I think it's best to java (as a verb) when you write Java, and to C++ when you C++! You'll appreciate why with experience of the language. Same as switching to any new language.

    0 讨论(0)
  • 2021-02-07 22:53

    If you want to implement all your methods inside your class definitions, you will have big monster header files (it's not a good idea to declare classes in cpp files) and possibly only one cpp file (the one that has your main function).

    So it's a matter of convenience.


    Edit:

    I find the declaration-implementation separation actually helpful. When I have monster classes with ~150 methods whose implementation is generated using lots of preprocessor tricks, I often want to be able to tell the difference between...

    • what the class can do (its interface, given by its declaration)
    • how the class does it (its implementation)

    Not being able to do that in either C# or Java is quite frustrating, especially when I don't have Intellisense at hand.

    0 讨论(0)
  • 2021-02-07 22:53

    If you are writing a C++ DLL and you put any code in the headers, you will end up with a hard to debug mess. The DLL will contain the code from the .cpp files but the users of the DLL will have some of the code inlined into themselves.

    This is really bad in Windows where you run into things like different heap allocators in different parts of the program and if you change the implementation, callers of your DLL will be using old code from the inlined headers.

    0 讨论(0)
  • 2021-02-07 22:55

    Separate declaration and definition is the least of the differences between C++ and Java. The major difference in modern C++ is the importance of "value semantics".

    Due to the lack of garbage collection but the excellent support for building types that behave like self-contained values, good C++ style involves writing well-behaved value types, with consistent construction, copy-construction, assignment, swap and destruction.

    Swap in particular is a very important one to remember because it's not directly supported by the language but it really should be there in any value-like type.

    Look at how the standard C++ library works, and how it expects your types to behave.

    Try to aim (not always possible) for a total absence of naked pointers or uses of the new operator. Hide such details inside classes that ensure they are used correctly, wrapping them up in value semantics.

    0 讨论(0)
  • 2021-02-07 22:56

    Most answers are about the separation of headers and compilation units. I agree with most, you must use it because it is more efficient, cleaner, more user friendly to coworkers or just because... (and you will notice the compilation time advantage not too long from now).

    Anyway I just wanted to post on the other part of the question: is C++ an OOP masacre?

    C++ is a multiparadigm language. It allows procedural, object oriented and generic programming. And that is a virtue, not a defect. You can still use pure OOP if you wish, but your code will surely benefit from learning and knowing when to use other paradigms.

    As an example, I dislike utility classes, where all member functions are static, and there is no data. There is no reason to create a utility class more than just grouping together a set of free functions under a common name. You must do it in Java, as it insists on pure OO syntax which, as commeted by Tom, is not the same as real OO. In C++ defining a namespace and a set of free functions offers a similar solution without you needing to lock the creation of objects by declaring a private constructor or else allowing users to instantiate non-sense objects from an empty class.

    To me, experience (I was once a Java-only programmer) has taught me that there are different tools that better fit different problems. I am yet to find a golden hammer and the advantage of C++ is that you can choose a different hammer for each different task, even in the same compilation unit.

    Correction: litb informs me in a comment that WNDCLASS is a struct in the win32 API. So that the criticism of the class not using initialization lists is plain nonsense and as such I am removing it.

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