{"id":2158,"date":"2015-01-29T03:28:05","date_gmt":"2015-01-29T02:28:05","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2158"},"modified":"2015-01-29T11:10:48","modified_gmt":"2015-01-29T10:10:48","slug":"2158","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2158","title":{"rendered":"Five Myths about C++ BUSTED!"},"content":{"rendered":"<p>This is a short of http:\/\/www.stroustrup.com\/Myths-final.pdf<\/p>\n<p><strong>Myth 1: \u201cTo understand C++, you must first learn C\u201d<\/strong><\/p>\n<blockquote><p>C is almost a subset of C++, but it is not the best subset to learn first because <strong>C lacks the notational<br \/>\nsupport, the type safety, and the easier-to-use standard library offered by C++ to simplify simple tasks<\/strong>.<br \/>\nConsider a trivial function to compose an email address:\n<\/p><\/blockquote>\n<p>Busted!<\/p>\n<p><strong>Myth 2: \u201cC++ is an Object-Oriented Language\u201d<\/strong><\/p>\n<blockquote><p>3. Myth 2: \u201cC++ is an Object-Oriented Language\u201d<br \/>\nC++ supports OOP and <strong>other programming styles<\/strong>, but is deliberately not limited to any narrow view<br \/>\nof \u201cObject Oriented.\u201d It supports a synthesis of programming techniques including <strong>object-oriented and<br \/>\ngeneric programming<\/strong>. More often than not, the best solution to a problem involves more than one style<br \/>\n(\u201cparadigm\u201d). By \u201cbest,\u201d I mean shortest, most comprehensible, most efficient, most maintainable, etc.\n<\/p><\/blockquote>\n<p>Busted!<\/p>\n<p><strong>Myth 3: \u201cFor reliable software, you need Garbage Collection\u201d<\/strong><br \/>\nIn short:<br \/>\n<strong>Never use new or delete<\/strong>. Use some classes which does the memory management for you, e.g. <strong>std::vector<\/strong>. For short living object, create them on the <strong>stack<\/strong>, Instead on the heap with new. Use <strong>destructor<\/strong> to clean up memory (and other resources like file handles) if the lifetime of an object comes to an end.<\/p>\n<p>If you have to pass an object around, <strong>don't use raw pointer<\/strong>. If it is possible, <strong>don't use pointer at all<\/strong>. If there exist only one owner at a time, use the <strong>move magic with rvalue reverences<\/strong>. If the object <strong>is shared by more than one owner, use std::shared_pointer<\/strong> to store a pointer. It implements a reference counting and deletes the object of nobody owns them anymore. Use <strong>auto<\/strong> and <strong>make_shared<><\/strong> for syntactic sugar.<\/p>\n<blockquote><p>Naked <strong>deletes<\/strong> are dangerous \u2013 and unnecessary in general\/user code. Leave <strong>deletes<\/strong><br \/>\ninside resource management classes, such as <strong>string, ostream, thread, unique_ptr<\/strong>, and shared_ptr.<\/p><\/blockquote>\n<blockquote><p>For resource management, I consider garbage collection a last choice, rather than \u201cthe solution\u201d or an ideal:<br \/>\n1.Use appropriate abstractions that recursively and implicitly handle their own resources. Prefer such objects to be scoped variables.<br \/>\n2. When you need pointer\/reference semantics, use \u201csmart pointers\u201d such as <strong>unique_ptr<\/strong> and<br \/>\n<strong>shared_ptr<\/strong> to represent ownership.<br \/>\n3. If everything else fails (e.g., because your code is part of a program using a mess of pointers<br \/>\nwithout a language supported strategy for resource management and error handling), try to<br \/>\nhandle non-memory resources \u201cby hand\u201d and plug in a conservative garbage collector to handle<br \/>\nthe almost inevitable memory leaks. <\/p><\/blockquote>\n<p>Busted!<\/p>\n<p><strong>Myth 4: \u201cFor efficiency, you must write low-level code\u201d<\/strong><\/p>\n<blockquote><p>I have seen <strong>sort<\/strong> run 10 times faster than <strong>qsort<\/strong>. How come? The C++ standard-library <strong>sort is<br \/>\nclearly at a higher level than qsort<\/strong> as well as <strong>more general and flexible<\/strong>. It is <strong>type safe<\/strong> and <strong>parameterized<br \/>\nover the storage type<\/strong>, <strong>element type<\/strong>, and <strong>sorting criteria<\/strong>. <strong>There isn\u2019t a pointer, cast, size, or a byte in<br \/>\nsight<\/strong>. The C++ standard library STL, of which sort is a part, <strong>tries very hard not to throw away information<\/strong>. This makes for <strong>excellent inlining and good optimizations<\/strong>.\n<\/p><\/blockquote>\n<blockquote><p>Generality and high-level code can beat low-level code. It doesn\u2019t always, of course, but the <strong>sort\/qsort<\/strong> comparison is not an isolated example. Always start out with a higher-level, precise, and type safe version of the solution. Optimize (only) if needed.\n<\/p><\/blockquote>\n<p>Busted!<\/p>\n<p><strong>Myth 5: \u201cC++ is for large, complicated, programs only\u201d<\/strong><\/p>\n<blockquote><p>C++ is a big language. The size of its definition is very similar to those of C# and Java. But that does not<br \/>\nimply that you have to know every detail to use it or use every feature directly in every program. <\/p><\/blockquote>\n<blockquote><p>After all, C++ is designed for flexibility and generality, but not every<br \/>\nprogram has to be a complete library or application framework.<\/p><\/blockquote>\n<p>Busted!<\/p>\n<p>Read the paper for detailed examples.<br \/>\nBusted!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a short of http:\/\/www.stroustrup.com\/Myths-final.pdf Myth 1: \u201cTo understand C++, you must first learn C\u201d C is almost a subset of C++, but it is not the best subset to learn first because C lacks the notational support, the type safety, and the easier-to-use standard library offered by C++ to simplify simple tasks. Consider [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17,31],"class_list":["post-2158","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp","tag-faster-code"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2158"}],"version-history":[{"count":3,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2158\/revisions"}],"predecessor-version":[{"id":2161,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2158\/revisions\/2161"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}