{"id":16538,"date":"2022-10-19T19:26:04","date_gmt":"2022-10-19T13:56:04","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/"},"modified":"2022-10-19T19:26:04","modified_gmt":"2022-10-19T13:56:04","slug":"solved-use-of-this-in-c-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/","title":{"rendered":"[Solved] Use of THIS-> in C\/C++ [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-13533990\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"13533990\" data-parentid=\"13533842\" data-score=\"6\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In this example, <code>THIS<\/code> is not a reference to a global variable; it is defined above in the function, as a cast of the void pointer <code>inRefCon<\/code>:<\/p>\n<pre><code>static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)\n{\n    \/\/ Get a reference to the object that was passed with the callback\n    \/\/ In this case, the AudioController passed itself so\n    \/\/ that you can access its data.\n    AudioController *THIS = (AudioController*)inRefCon;\n<\/code><\/pre>\n<p>This is a fairly common pattern in C; in order to pass a callback in to some API, so that it can later call your code, you pass both a function pointer and a void pointer. The void pointer contains whatever data your function pointer will need to operate on. Within your callback, you will need to cast it back to a pointer to the actual type, so you can access the data within it. In this case, the author of the example is naming that cast pointer <code>THIS<\/code>, probably to make this look more object-oriented, even though this is just C and <code>THIS<\/code> has no special meaning.<\/p>\n<p>You ask why they assign it to a local variable rather than just using <code>THIS-&gt;sinPhase<\/code> everywhere. There&#8217;s no reason you couldn&#8217;t use <code>THIS-&gt;sinPhase<\/code> everywhere; they likely just assigned it to a local variable <code>phase<\/code> to save on typing. There&#8217;s a small chance that the optimizer could do a better job on a local variable than on one passed in via a pointer, because it can make more assumptions about the local variable (in particular, it can assume that no one else is updating it at the same time). So the loop might run slightly faster using a local variable, though I wouldn&#8217;t be certain without testing; the most likely reason is just to save typing and make the code more readable.<\/p>\n<p>Here&#8217;s a simplified example of how a callback API like this works; hopefully this should make it easier to understand how a callback API works, without trying to understand the rest of what&#8217;s going on in Core Audio at the same time. Let&#8217;s say I want to write a function that will apply a callback to an integer 10 times. I might write:<\/p>\n<pre><code>int do_ten_times(int input, int (*callback)(int)) {\n    int value = input;\n    for (int i = 0; i &lt; 10; ++i) {\n        value = callback(value);\n    }\n    return value;\n}\n<\/code><\/pre>\n<p>Now I could call this with different functions, like the following <code>add_one()<\/code> or <code>times_two()<\/code>:<\/p>\n<pre><code>int add_one(int x) {\n    return x + 1;\n}\n\nint times_two(int x) {\n    return x * 2;\n}\n\nresult = do_ten_times(1, add_one);\nresult = do_ten_times(1, times_two);\n<\/code><\/pre>\n<p>But say I want to be able to add or multiply by different numbers; I could try writing one function for each number that you wanted to add or multiply by, but then you would run into a problem if the number wasn&#8217;t fixed in the code, but was based on input. You can&#8217;t write one function for each possible number; you are going to need to pass a value in. So let&#8217;s add a value to our callbacks, and have <code>do_ten_times()<\/code> pass that value in:<\/p>\n<pre><code>int do_ten_times(int input, int (*callback)(int, int), int data) {\n    int value = input;\n    for (int i = 0; i &lt; 10; ++i) {\n        value = callback(value, data);\n    }\n    return value;\n}\n\nint add(int x, int increment) {\n    return x + increment;\n}\n\nint times(int x, int multiplier) {\n    return x * multiplier;\n}\n\nresult = do_ten_times(1, add, 3);\nresult = do_ten_times(1, times, 4);\n<\/code><\/pre>\n<p>But what if someone wants to write a function that varies by something other than an integer? For instance, what if you want to write a function that will add different numbers depending on whether the input is negative or positive? Now we need to pass two values in. Again, we could extend our interface to pass in two values; but we will eventually need to pass in more values, values of different types, and the like. We notice that <code>do_ten_times<\/code> really doesn&#8217;t care about the type of the value we&#8217;re passing in; it just needs to pass it to the callback, and the callback can interpret it however it likes. We can achieve this with a void pointer; the callback then casts that void pointer to the appropriate type to get the value out:<\/p>\n<pre><code>int do_ten_times(int input, int (*callback)(int, void *), void *data) {\n    int value = input;\n    for (int i = 0; i &lt; 10; ++i) {\n        value = callback(value, data);\n    }\n    return value;\n}\n\nint add(int x, void *data) {\n    int increment = *(int *)data;\n    return x + increment;\n}\n\nint times(int x, void *data) {\n    int multiplier = *(int *)data;\n    return x * multiplier;\n}\n\nstruct pos_neg {\n    int pos;\n    int neg;\n};\nint add_pos_neg(int x, void *data) {\n    struct pos_neg *increments = (struct pos_neg *)data;\n    if (x &gt;= 0)\n        return x + increments-&gt;pos;\n    else\n        return x + increments-&gt;neg;\n}\n\nint i = 3;\nresult = do_ten_times(1, add, &amp;i);\nint m = 4;\nresult = do_ten_times(1, times, &amp;m);\nstruct pos_neg pn = { 2, -2 };\nresult = do_ten_times(-1, add_pos_neg, &amp;pn);\n<\/code><\/pre>\n<p>These are all, of course, toy examples. In the Core Audio case, the callback is used to generate a buffer of audio data; it is called every time the audio system needs to generate more data in order to keep playing smoothly. The information passed via the <code>void *inRefCon<\/code> is used to track how exactly where in the sine wave you have gotten to in the current buffer, so the next buffer can pick up where the last one left off.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Use of THIS-> in C\/C++ [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In this example, THIS is not a reference to a global variable; it is defined above in the function, as a cast of the void pointer inRefCon: static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { \/\/ Get a reference to the object that was passed with &#8230; <a title=\"[Solved] Use of THIS-&gt; in C\/C++ [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\" aria-label=\"More on [Solved] Use of THIS-&gt; in C\/C++ [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,470],"class_list":["post-16538","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-objective-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Use of THIS-&gt; in C\/C++ [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Use of THIS-&gt; in C\/C++ [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In this example, THIS is not a reference to a global variable; it is defined above in the function, as a cast of the void pointer inRefCon: static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { \/\/ Get a reference to the object that was passed with ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-19T13:56:04+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Use of THIS-> in C\/C++ [closed]\",\"datePublished\":\"2022-10-19T13:56:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\"},\"wordCount\":651,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"objective-c\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\",\"name\":\"[Solved] Use of THIS-> in C\/C++ [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-19T13:56:04+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Use of THIS-> in C\/C++ [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Use of THIS-> in C\/C++ [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Use of THIS-> in C\/C++ [closed] - JassWeb","og_description":"[ad_1] In this example, THIS is not a reference to a global variable; it is defined above in the function, as a cast of the void pointer inRefCon: static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { \/\/ Get a reference to the object that was passed with ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-19T13:56:04+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Use of THIS-> in C\/C++ [closed]","datePublished":"2022-10-19T13:56:04+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/"},"wordCount":651,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","objective-c"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/","name":"[Solved] Use of THIS-> in C\/C++ [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-19T13:56:04+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-c-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Use of THIS-> in C\/C++ [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16538"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16538\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}