{"id":27983,"date":"2007-02-16T10:00:03","date_gmt":"2007-02-16T10:00:03","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/02\/16\/why-dont-i-use-any-class-libraries-in-my-sample-code\/"},"modified":"2007-02-16T10:00:03","modified_gmt":"2007-02-16T10:00:03","slug":"why-dont-i-use-any-class-libraries-in-my-sample-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070216-03\/?p=27983","title":{"rendered":"Why don&#039;t I use any class libraries in my sample code?"},"content":{"rendered":"<p>As a general rule, I avoid using any class libraries in my sample code. This isn&#8217;t because I&#8217;m opposed to class libraries, but rather because I don&#8217;t want to narrow my audience to &#8220;people who use MFC&#8221; (to choose one popular class library). If I were to start using MFC for all of my samples, I&#8217;d probably lose all the people who don&#8217;t use MFC.\n &#8220;Oh, but those people can just translate the MFC code into whatever class library they use.&#8221;\n Well, sure, they could do that, but first they would have to learn MFC. I wouldn&#8217;t be talking about <code>HWND<\/code>s and <code>HDC<\/code>s any more but rather <code>CWnd<\/code>s and <code>CDC<\/code>s. I would write &#8220;Add this to your <code>OnDropEx<\/code> handler&#8221;, and all the non-MFC people would say, &#8220;What are you talking about? I&#8217;m not using MFC. What is the Win32 equivalent to <code>OnDropEx<\/code>?&#8221; (Suppose my article on <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/04\/23\/118893.aspx\"> using accessibility to read the text under the mouse cursor<\/a> were titled &#8220;How to use MFC to retrieve text under the mouse cursor.&#8221; Would you have read it?)\n &#8220;Well, fine, don&#8217;t use MFC, but still it wouldn&#8217;t kill you to use a smart pointer library.&#8221;\n But which one? There&#8217;s MFC&#8217;s <code>CIP<\/code>, ATL&#8217;s <code>CComPtr<\/code>, STL^H^H^Hthe C++ standard library&#8217;s <code>auto_ptr<\/code>, the Microsoft compiler&#8217;s built-in <code>_com_ptr_t<\/code> (which you get automatically if you use the nonstandard <code>#import<\/code> directive), and boost&#8217;s grab bag of smart pointer classes <code>scoped_ptr<\/code>, <code>shared_ptr<\/code>, <code>weak_ptr<\/code>, <code>intrusive_ptr<\/code>&#8230; And they all behave differently. Sometimes subtly incompatibly. For example, MFC&#8217;s <code>CIP::CreateObject<\/code> method uses <code>CLSCTX_INPROC_SERVER<\/code>, whereas ATL&#8217;s <code>CComPtr::CreateInstance<\/code> method uses <code>CLSCTX_ALL<\/code>. When you&#8217;re chasing down a nasty COM marshalling problem, these tiny details matter, and if you&#8217;re an ATL programmer looking at MFC code, these tiny details are also something you&#8217;re going to miss simply due to lack of familiarity. (And woe unto you if your preferred language is VB or C# or some other popular non-C++ language. Now you have double the translation work ahead of you.)\n Instead of hiding the subtleties behind a class library, I put them right out on the table. Those of you who have a favorite class library can convert the boring error-prone plain C++ code into your beautiful class library.\n In fact, I almost expect you to do it.<\/p>\n<p> (On a related note, some people are horrified at the rather dense code presentation I use here. I don&#8217;t write code like that in real life; I&#8217;d be just as horrified as you if I saw that code in a real program. I just use that style here because of the nature of the medium. A great way to lose people&#8217;s interest is to make them plow through 100 lines of boring code before they reach the good stuff.) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a general rule, I avoid using any class libraries in my sample code. This isn&#8217;t because I&#8217;m opposed to class libraries, but rather because I don&#8217;t want to narrow my audience to &#8220;people who use MFC&#8221; (to choose one popular class library). If I were to start using MFC for all of my samples, [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-27983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>As a general rule, I avoid using any class libraries in my sample code. This isn&#8217;t because I&#8217;m opposed to class libraries, but rather because I don&#8217;t want to narrow my audience to &#8220;people who use MFC&#8221; (to choose one popular class library). If I were to start using MFC for all of my samples, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/27983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=27983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/27983\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=27983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=27983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=27983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}