{"id":2780,"date":"2022-01-07T20:21:48","date_gmt":"2022-01-07T11:21:48","guid":{"rendered":"https:\/\/www.codedojo.com\/?p=2780"},"modified":"2023-06-10T12:12:16","modified_gmt":"2023-06-10T03:12:16","slug":"adding-a-cool-custom-c-class-template-to-visual-studio-2022","status":"publish","type":"post","link":"https:\/\/www.codedojo.com\/?p=2780","title":{"rendered":"Adding a cool custom C++ class template to Visual Studio 2022"},"content":{"rendered":"\n<p>Ok, this is one of those posts that are more just to document something so the next time I install VS on a computer I remember how to do this and can find the file. <\/p>\n\n\n\n<p>If you don&#8217;t use Visual Studio C++ then <strong>you should run<\/strong>.   Or, prepare to dive into the <strong>incredibly boring<\/strong> world of the lengths programmers will go to just to avoid monotonous typing.<\/p>\n\n\n\n<p>I&#8217;m all about classes.  <strong>When you think of classy, think of me. <\/strong> Any time I do anything, I need a .cpp and.h file for it.   And I want &#8217;em setup pretty with the current date and stuff.  Automagically!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"765\" src=\"https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template1-1024x765.png\" alt=\"\" class=\"wp-image-2781\" srcset=\"https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template1-1024x765.png 1024w, https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template1-300x224.png 300w, https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template1-768x574.png 768w, https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template1-624x466.png 624w, https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template1.png 1044w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">See that? If I specify &#8220;GameProfileManager&#8221;, these two files get made with these contents.  That&#8217;s all I&#8217;m trying to do, as I make a LOT OF CLASSES.<\/figcaption><\/figure>\n\n\n\n<p>For years I used custom macros I&#8217;d made for Visual Assist, but at $279 + $119 yearly it&#8217;s a bit hard to continue to justify using VA when VS 2022 has most of its features built in these days.  <\/p>\n\n\n\n<p>Yes, you can pay less but rules like the payment &#8220;<em>Cannot be reimbursed by a company or organization<\/em>&#8221; and the world&#8217;s most annoying licensing system makes it not worth it.<\/p>\n\n\n\n<p>So what&#8217;s a lad to do?  Well, I finally got off my bum (well, on it, I guess) and learned how to have the same macros with built-in features that VS has had forever.  They work a tiny bit different but close enough.  <\/p>\n\n\n\n<p><strong>Hint: <\/strong><em>Don&#8217;t even try to use the &#8220;Snippets&#8221; feature to do this, it won&#8217;t work.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to add<\/h2>\n\n\n\n<p>Download<strong> <a href=\"\/files\/SethClassTemplate.zip\">this file<\/a><\/strong> and place it in your <strong>%USERPROFILE%\\Documents\\Visual Studio 2022\\Templates\\ItemTemplates\\Visual C++<\/strong> dir.<\/p>\n\n\n\n<p> (or <strong>%USERPROFILE%\\Documents\\Visual Studio 2019\\Templates<\/strong> if you&#8217;re using VS 2019&#8230; I think)<\/p>\n\n\n\n<p><strong>DON&#8217;T UNZIP!<\/strong>  VS reads it as a zip.<\/p>\n\n\n\n<p>Close and restart Visual Studio.<\/p>\n\n\n\n<p>Then add new classes using the &#8220;Add item&#8221; menu.  Like this shittily produced unlisted video shows: (can you guess the music I used?  Why did this even need music? Are you wondering what youtube video I was watching?)<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Adding a cool custom C++ class template to Visual Studio 2022\" width=\"625\" height=\"352\" src=\"https:\/\/www.youtube.com\/embed\/ekw4zXaNg1I?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to change it<\/h2>\n\n\n\n<p>Great!  Ok, but you probably wanted to customize it with your own name and <em>not<\/em> give me credit to everything you make.  Well, fine, if you must.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"317\" src=\"https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template2.png\" alt=\"\" class=\"wp-image-2782\" srcset=\"https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template2.png 649w, https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template2-300x147.png 300w, https:\/\/www.codedojo.com\/wp-content\/uploads\/2022\/01\/vs_template2-624x305.png 624w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/figure>\n\n\n\n<p>To modify it with your own name\/email\/texts, just unzip the file you downloaded, you&#8217;ll see four files inside.  Delete the original .zip.  (you&#8217;ll make your own in a sec)<\/p>\n\n\n\n<p>Edit the .cpp and .h as you like, then zip the four files up again. (I don&#8217;t think the name matters, it just has to be in the same directory as before)   Restart VS and you should see the changes.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>When editing, there are many special words you can use that get replaced automatically (<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/template-parameters?view=vs-2022\">full list here<\/a>).  <\/p>\n\n\n\n<p>Some useful ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$safeitemname$<\/strong> &#8211; (current filename without the .cpp or .h part)<\/li>\n\n\n\n<li><strong>$projectname$<\/strong> &#8211; The name of the project this is in<\/li>\n\n\n\n<li><strong>$year$<\/strong> &#8211; Current year (ie, 2022)<\/li>\n\n\n\n<li><strong>$time$<\/strong> &#8211; The current time in the format DD\/MM\/YYYY 00:00:00<\/li>\n\n\n\n<li><strong>$username$<\/strong> &#8211; The current user name<\/li>\n\n\n\n<li><strong>$registeredorganization$<\/strong> &#8211; The registry key value from <strong>HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\RegisteredOrganization<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>Hint:<\/strong> <em>Edit the MyTemplate.vstemplate file to change the name and some other things.  Useful if you want multiple templates, one for component classes, etc.<\/em><\/p>\n\n\n\n<p>And that&#8217;s it!<strong>  Suck it, Visual Assist. <\/strong> But also I love you so please get cheaper and less annoying to buy &amp; license.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ok, this is one of those posts that are more just to document something so the next time I install VS on a computer I remember how to do this and can find the file. If you don&#8217;t use Visual Studio C++ then you should run. Or, prepare to dive into the incredibly boring world [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-2780","post","type-post","status-publish","format-standard","hentry","category-development","category-tech-tips"],"_links":{"self":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/2780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2780"}],"version-history":[{"count":12,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/2780\/revisions"}],"predecessor-version":[{"id":3174,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/2780\/revisions\/3174"}],"wp:attachment":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}