Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some do one .h/.cpp per class:</p> <h3>my.h</h3> <pre><code>#ifndef MY_H #define MY_H #include "other.h" class MyClass { public: void MyMethod(); OtherClass test; } #endif // MY_H </code></pre> <h3>other.h</h3> <pre><code>#ifndef OTHER_H #define OTHER_H class OtherClass { public: void Othermethod(); } #endif // OTHER_H </code></pre> <h3>my.cpp</h3> <pre><code>#include "my.h" void MyClass::MyMethod() { } </code></pre> <h3>other.cpp</h3> <pre><code>#include "other.h" #include "my.h" void OtherClass::OtherMethod)() { // (ab)using MyClass... } </code></pre> <p>If you're using only Visual Studio, you could use <code>#pragma once</code> instead of <code>#ifndef xx_h #define xx_h #endif // xx_h</code>. EDIT: as the comment says, and also the related <a href="http://en.wikipedia.org/wiki/Pragma_once" rel="nofollow noreferrer">Wikipedia page</a>, <code>#pragma once</code> is also supported (at least) by GCC.</p> <p>UPDATE: About the updated question, unrelated to #include, but more about passing objects around...</p> <p>MyClass already has an embedded instance of OtherClass, <code>test</code>. So, in MyMethod, it's probably more like:</p> <pre><code>void MyClass::MyMethod() { test.OtherMethod(); } </code></pre> <p>And if OtherMethod needs to access the MyClass instance, pass this instance to OtherMethod either as a reference, or a pointer:</p> <h3>By reference</h3> <pre><code>class OtherClass { public: void OtherMethod(MyClass &amp;parent); } void MyClass::MyMethod() { test.OtherMethod(*this); } void OtherClass::OtherMethod(MyClass &amp;parent) { parent.AnotherMethod(); } </code></pre> <h3>By Pointer</h3> <pre><code>class OtherClass { public: void OtherMethod(MyClass *parent); } void MyClass::MyMethod() { test.OtherMethod(this); } void OtherClass::OtherMethod(MyClass *parent) { if (parent == NULL) return; // or any other kind of assert parent-&gt;AnotherMethod(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload