{"id":10322,"date":"2022-09-23T08:35:43","date_gmt":"2022-09-23T03:05:43","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/"},"modified":"2022-09-23T08:35:43","modified_gmt":"2022-09-23T03:05:43","slug":"solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/","title":{"rendered":"[Solved] How can I use a class from a header file in a source file using extern but not #include?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-1066423\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"1066423\" data-parentid=\"1065211\" data-score=\"5\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" 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>Just to clarify.  It is impossible to <code>extern<\/code> the class:<\/p>\n<pre><code>class Outside\n{\n    public:\n        Outside(int count);\n        GetCount();\n}\n<\/code><\/pre>\n<p>But, once you have the class available in framework.cpp, you <strong>CAN<\/strong> <code>extern<\/code> an object of type <code>Outside<\/code>.  You&#8217;ll need a .cpp file declaring that variable:<\/p>\n<pre><code>#include \"outside.h\"\n\nOutside outside(5);\n<\/code><\/pre>\n<p>And then you can refer to that object in another file via <code>extern<\/code> (as long as you link in the correct object file when you compile your project):<\/p>\n<pre><code>#include \"outside.h\"\n\nextern Outside outside;\nint current_count = outside.GetCount();\n<\/code><\/pre>\n<p><code>extern<\/code> is used to say &#8220;I <strong>KNOW<\/strong> a variable of this type with this name will exist when this program runs, and I want to use it.&#8221;  It works with variables\/objects, not classes, structs, unions, typedefs, etc.  It&#8217;s not much different from <code>static<\/code> objects.<\/p>\n<p>You may be thinking about forward declaring classes to cut down on compile times, but there are restrictions on that (you only get to use the objects as opaque pointers and are not able to call methods on them).<\/p>\n<p>You may also mean to hide the implementation of <code>Outside<\/code> from users.  In order to do that, you&#8217;re going to want to read up on the PIMPL pattern.<\/p>\n<hr>\n<h3>Update<\/h3>\n<p>One possibility would be to add a free function to Outside.h (I&#8217;ve also added a namespace):<\/p>\n<pre><code>namespace X {\n    class Outside {\n        int count_;\n        public:\n            Outside(int count) : count_(count) { }\n            int GetCount()\n            {\n                return count_;\n            }\n    };\n\n    int GetOutsideCount(Outside* o);\n}\n<\/code><\/pre>\n<p>Implement that function in a .cpp file.  While you&#8217;re at it, you might as well make the global variable that you intend to <code>extern<\/code> (note, the variable itself does not need to be a pointer):<\/p>\n<pre><code>#include \"outside.h\"\n\nnamespace X {\n    int GetOutsideCount(Outside* o)\n    {\n        return o-&gt;GetCount();\n    }\n}\n\nX::Outside outside(5);\n<\/code><\/pre>\n<p>And then do this in your program (note that you cannot call any methods on <code>outside<\/code> because you did not include outside.h and you don&#8217;t want to violate the one definition rule by adding a new definition of the class or those methods; but since the definitions are unavailable you&#8217;ll need to pass pointers to <code>outside<\/code> around and not <code>outside<\/code> itself):<\/p>\n<pre><code>namespace X {\n    class Outside;\n    int GetOutsideCount(Outside* o);\n}\n\nextern X::Outside outside;\n\nint main()\n{\n    int current_count = GetOutsideCount(&amp;outside);\n}\n<\/code><\/pre>\n<p>I consider this an abomination, to put it mildly.  Your program will find the <code>GetOutsideCount<\/code> function, call it by passing it an <code>Outside*<\/code>.  <code>Outside::GetCount<\/code> is actually compiled to a normal function that takes a secret <code>Outside<\/code> object (inside <code>Outside::GetCount<\/code> that object is referred to via the <code>this<\/code> pointer), so <code>GetOutsideCount<\/code> will find that function, and tell it to dereference the <code>Outside*<\/code> that was passed to <code>GetOutsideCount<\/code>.  I think that&#8217;s called &#8220;going the long way &#8217;round.&#8221;<\/p>\n<p>But it is what it is.<\/p>\n<p>If you aren&#8217;t married to using the <code>extern<\/code> keyword, you can instead go full &#8220;let&#8217;s use C++ like it&#8217;s C&#8221; mode by adding the following two functions in the same way (i.e., via forward declarations and implementing right next to <code>int GetOUtsideCount()<\/code>:<\/p>\n<pre><code>Outside* CreateOutsidePointer(int count)\n{\n    return new Outside(count);\n}\n\nvoid DestroyOutsidePointer(Outside* o)\n{\n    return delete o;\n}\n<\/code><\/pre>\n<p>I&#8217;m more willing to swallow that.  It&#8217;s a lot like the strategy used by the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/svn.apache.org\/repos\/asf\/apr\/apr\/trunk\/docs\/incomplete_types\">APR<\/a>.<\/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 How can I use a class from a header file in a source file using extern but not #include? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Just to clarify. It is impossible to extern the class: class Outside { public: Outside(int count); GetCount(); } But, once you have the class available in framework.cpp, you CAN extern an object of type Outside. You&#8217;ll need a .cpp file declaring that variable: #include &#8220;outside.h&#8221; Outside outside(5); And then you can refer to that &#8230; <a title=\"[Solved] How can I use a class from a header file in a source file using extern but not #include?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\" aria-label=\"More on [Solved] How can I use a class from a header file in a source file using extern but not #include?\">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,2838],"class_list":["post-10322","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-extern"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How can I use a class from a header file in a source file using extern but not #include? - 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-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How can I use a class from a header file in a source file using extern but not #include? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Just to clarify. It is impossible to extern the class: class Outside { public: Outside(int count); GetCount(); } But, once you have the class available in framework.cpp, you CAN extern an object of type Outside. You&#8217;ll need a .cpp file declaring that variable: #include &quot;outside.h&quot; Outside outside(5); And then you can refer to that ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-23T03:05:43+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How can I use a class from a header file in a source file using extern but not #include?\",\"datePublished\":\"2022-09-23T03:05:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\"},\"wordCount\":463,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"extern\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\",\"name\":\"[Solved] How can I use a class from a header file in a source file using extern but not #include? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-23T03:05:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How can I use a class from a header file in a source file using extern but not #include?\"}]},{\"@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=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How can I use a class from a header file in a source file using extern but not #include? - 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-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How can I use a class from a header file in a source file using extern but not #include? - JassWeb","og_description":"[ad_1] Just to clarify. It is impossible to extern the class: class Outside { public: Outside(int count); GetCount(); } But, once you have the class available in framework.cpp, you CAN extern an object of type Outside. You&#8217;ll need a .cpp file declaring that variable: #include \"outside.h\" Outside outside(5); And then you can refer to that ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/","og_site_name":"JassWeb","article_published_time":"2022-09-23T03:05:43+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How can I use a class from a header file in a source file using extern but not #include?","datePublished":"2022-09-23T03:05:43+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/"},"wordCount":463,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","extern"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/","url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/","name":"[Solved] How can I use a class from a header file in a source file using extern but not #include? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-23T03:05:43+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-a-class-from-a-header-file-in-a-source-file-using-extern-but-not-include\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How can I use a class from a header file in a source file using extern but not #include?"}]},{"@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=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","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\/10322","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=10322"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10322\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=10322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=10322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=10322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}