Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ inheritance pointer
    text
    copied!<p>it's pretty diffecult for me to describe my problem. I have two classes, I would say <code>Base_A</code> and <code>Derived_A</code>. You can see from the names, the class <code>Derived_A</code> is derived from <code>Base_A</code>. Also in my program I have other two classes <code>Base_B</code> and <code>Derived_B</code> (also with inheritance). The class <code>Base_A</code> contains the object of <code>Base_B</code>, and the class <code>Derived_A</code> contains the object of <code>Derived_B</code>. </p> <pre><code>class Base_A { public: Base_A() {} virtual ~Base_A() {} Base_B b_; Base_B* pointer_; void init() { b_ = Base_B(); pointer_ = &amp;b_; pointer_-&gt;setValue(1); } void print() { pointer_-&gt;getValue(); } }; class Derived_A: public Base_A { public: Derived_A() {} virtual ~Derived_A() {} Derived_B b_; Derived_B* pointer_; void init() { b_ = Derived_B(); pointer_ = &amp;b_; pointer_-&gt;setValue(2); pointer_-&gt;increaseValue(); } }; class Base_B { public: Base_B() {} virtual ~Base_B() {} int value_; void setValue(int value) { value_ = value; } void getValue() { cout &lt;&lt; "Base_B: " &lt;&lt; value_ &lt;&lt; endl; } }; class Derived_B: public Base_B { public: Derived_B() {} virtual ~Derived_B() {} void increaseValue() { value_++; } }; int main() { Derived_A derived_A = Derived_A(); derived_A.init(); derived_A.print(); return 0; } </code></pre> <p>How you can see every class of A has one object of class B and pointer to this object. My problem is, when I call the function <code>print()</code>, it does not take <code>Derived_B* pointer_</code>, but try to access <code>Base_B* pointer_</code>, which is not exist. How I can say in my program, that it should take the pointer according to the class? Or do I need to declarate the <code>Base_B* pointer_</code> inside the <code>Derived_A</code> class like:</p> <pre><code>Base::pointer_ = pointer_; </code></pre> <p>Maybe is there other method or algorithm for my problem?</p> <p>Thank you a lot.</p>
 

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