Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Suppose you have a class A, like this:</p> <pre><code>class A { public: B my_b_member; }; </code></pre> <p>that needs this class B:</p> <pre><code>class B { public: int my_integer; }; </code></pre> <p>Of course, the class A will need the entire definition of B, at least the part that describes B's constructor. Why ? When you'll declare your A class, one of it's member (which is B) will be instantiated as well, using its default constructor(with no argument). So every class, or file in your project that will need an A class, will also have to include the B class definition before.</p> <p>The big advantage of having a class A like this instead:</p> <pre><code>class A { public: B* my_b_member; }; </code></pre> <p>is that you will only need to declare that a <strong>B class</strong> exists, with :</p> <p><code>class B;</code></p> <p>before A's definition.</p> <p>The only file that will need B's entire definition is the file which will contain the B's class instantiation, for example:</p> <pre><code>int main(void) { A a; a.my_b_member = new B; return 0; } </code></pre> <p>So it depends on your project, what you are trying to achieve, etc etc. I wouldn't say every class members <strong>have</strong> to be pointers, because having to instantiate each member your class contains by allocating them means that these members will be allocated on the heap, instead of the stack. In some cases you wouldn't want this to happen:</p> <p>When you are manipulating network chunks, sending pointers is just impossible.</p>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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