Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, the only difference between a <code>class</code> and a <code>struct</code> in C++ is that a class' members are <code>private</code> by default and a struct's members are <code>public</code> by default. Compiler-generated ctors and dtors are visible in both cases - unless otherwise stated by the programmer (e.g. they move the default ctor into a <code>private</code> section). Otherwise construction and destruction of instances of user-defined types marked <code>class</code> wouldn't be possible without explicit, public declaration - thus defying the very purpose of compiler-generated functions.</p> <p>So basically, what you do in your example is merely <em>composition</em> of two user defined types which is perfectly legal. When you create an instance of <code>capture</code>, an instance of <code>test</code> is created as well.</p> <p>What you <em>can't</em> do is publicly access <code>AttachToInput()</code> from outside of <code>test</code> and <em>derived</em> types of <code>test</code>. You need to declare the function <code>public</code> in order for this line to compile:</p> <pre><code>h_cap.h_app.AttachToInput(); // error: member function of `test` is protected </code></pre> <p>On another, unrelated note (but I came across it so I mention it), your <code>class test</code> holds a raw pointer to <code>char</code>. Holding raw pointers is ok, if the lifetime of the entity that's being pointed is guaranteed to exceed the lifetime of the object that holds the pointer. Otherwise, it's very likely the object itself is responsible for the destruction of said entity. You need to be sure about who owns what and who's responsible for allocation and deallocation of stuff.</p> <p><strong>EDIT</strong>: It should be noted, that Alan Stokes proposed the same in the comment section while I wrote this answer. :)</p> <p><strong>EDIT2</strong>: Slight oversight, implicit default access is also assumed for base classes depending on how the <em>derived</em> class is declared. See <a href="https://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c">What are the differences between struct and class in C++?</a>.</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. 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