{"id":226,"date":"2014-01-09T13:27:20","date_gmt":"2014-01-09T20:27:20","guid":{"rendered":"http:\/\/www.earlevel.com\/main\/?p=226"},"modified":"2019-02-24T15:03:31","modified_gmt":"2019-02-24T23:03:31","slug":"more-about-source-code","status":"publish","type":"post","link":"https:\/\/www.earlevel.com\/main\/2014\/01\/09\/more-about-source-code\/","title":{"rendered":"More about source code"},"content":{"rendered":"<p>I was admonished (in not a nice way\u2014not terribly rude, but quite to the point that I don&#8217;t know what I&#8217;m doing) by an anonymous visitor, who concluded that I don&#8217;t know much about C++ and maybe should have written it in a different language (why, when the purpose is to give people C++ code?). I would reply to him if he had left a real email address, but maybe I should make a few things clear to all.<\/p>\n<p>I supply bare bones code. I try to leave out anything not necessary, and avoid even fairly obvious optimizations (and some less obvious that would make the code more difficult to understand. For instance, the wavetables could be one sample larger and let you avoid testing for wrap-around and the resulting branch). See <a href=\"\/main\/2013\/03\/03\/about-source-code-examples\/\" title=\"About source code examples\">About source code examples<\/a>.<\/p>\n<p>There is a common misconception that if your class is suitable for subclassing, then the destructor must be declared virtual. This is not true. If it were, the C++ architects would have simply built it in.<\/p>\n<p>So, to some (including the anonymous commenter), it may seem confusing that I don&#8217;t declare destructors as virtual, which they might conclude to imply no subclassing, while declaring variables as protected (not private), implying support for subclassing. There is a method to this apparent madness: First, I don&#8217;t expect that the object will be subclassed by you, and I want to keep it efficient. So, I don&#8217;t declare destructors virtual, which would create some overhead we don&#8217;t need. However, if you do choose to subclass, then it&#8217;s important that you have access to certain variables, hence the protected declarations where appropriate.<\/p>\n<p>However, that fact is that base class destructors need not be virtual when subclassed\u2014that&#8217;s a requirement only if you want to delete them via the base class. By not declaring them virtual, we get the lack of vtable in the typically case (of no subclasses), while still allowing subclassing.<\/p>\n<p>Also, I don&#8217;t mess with namespaces, and avoid templates even though they would be useful\u2014again, trying to focus on the code that implements the DSP algorithm. I try to fit as much of the pertinent code on the screen for you as possible, so while I like comments in my code, I keep them brief.<\/p>\n<p>And I don&#8217;t go through and declare every non-changing variable as const. This is not open source code. I&#8217;m not trying to protect you or some group you are working on code with from yourselves. I&#8217;m giving you seeds, not a framework, and it&#8217;s not in a Git repository. If I keep it simple enough, I figure you&#8217;ll be able to pull the bits out that matter for you into your own code. I don&#8217;t have time for both open source and tutorials\u2014you can find a ton of open source oscillators, filters, and modulators on the internet, and I think you&#8217;ll find that the authors do not teach you how and why the code works. The latter is few and far between, and that&#8217;s why I&#8217;m here.<\/p>\n<p>I&#8217;m open to suggestions, but realize that I do think about my choices, even if it&#8217;s not apparent. So, please, let&#8217;s make it a discussion. And the anonymous fellow is invited to point us to his contributions to the public. \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was admonished (in not a nice way\u2014not terribly rude, but quite to the point that I don&#8217;t know what I&#8217;m doing) by an anonymous visitor, who concluded that I don&#8217;t know much about C++ and maybe should have written &hellip; <a href=\"https:\/\/www.earlevel.com\/main\/2014\/01\/09\/more-about-source-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/226"}],"collection":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":1,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":820,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/226\/revisions\/820"}],"wp:attachment":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}