C++ programming style

前端 未结 16 1688
眼角桃花
眼角桃花 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: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.

提交回复
热议问题