Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll try to strip away the details of your model and focus on the following abstract design:</p> <pre><code> struct A struct DA : A { { }; }; //============================================================================== struct B struct DB : B { { A* p; DA* pD; // Avoid this }; }; </code></pre> <p>Now what you are trying to do is to avoid adding an <em>extra</em> member variable to <code>DB</code>, and be able to treat the pointer <code>p</code> inherited from <code>B</code> as if it were of type <code>DA*</code>.</p> <p>You can structure classes <code>B</code> and <code>DB</code> this way:</p> <pre><code>struct B { private: A* p; public: B(A* _p) : p(_p) { } A* get_p() { return p; } } struct DB : B { public: B(DA* _p) : B(_p) { } DA* get_p() { return static_cast&lt;DA*&gt;(A::get_p()); } } </code></pre> <p>Superclass <code>B</code> will hold a pointer of type <code>A*</code>. This pointer is set at construction time. If the constructor is invoked while creating an instance of <code>DB</code>, a pointer to an object of type <code>DA</code> will be stored. <code>DB</code> provides a function <code>get_p()</code> that hides <code>B</code>'s version of <code>get_p()</code> and returns a (properly casted) pointer of type <code>DA*</code>. </p> <p>Due to this design, the <code>static_cast&lt;&gt;</code> in <code>DB::get_p()</code> is guaranteed to be safe (unless you use virtual inheritance, in which case you should use the less efficient <code>dynamic_cast&lt;&gt;</code>).</p> <p>Internal operations of <code>B</code> would access the pointer <code>p</code> directly. Clients of <code>B</code> would retrieve it through a call to <code>B::get_p()</code>. Inside of <code>DB</code>, and for clients of <code>DB</code> as well, you would access the object pointed to by <code>p</code> by retrieving the pointer through function <code>B::get_p()</code>, rather than directly dereferencing <code>p</code>.</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