Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you return a pointer to a base class with a virtual function?
    primarykey
    data
    text
    <p>I have a base class called Element, a derived class called Vector, and I'm trying to redefine two virtual functions from Element in Vector.</p> <pre><code>//element.h template &lt;class T&gt; class Element { public: Element(); virtual Element&amp; plus(const Element&amp;); virtual Element&amp; minus(const Element&amp;); }; </code></pre> <p>and in another file</p> <pre><code>//Vector.h #include "Element.h" template &lt;class T&gt; class Vector: public Element&lt;T&gt; { T x, y, z; public: //constructors Vector(); Vector(const T&amp; x, const T&amp; y = 0, const T&amp; z =0); Vector(const Vector&amp; u); ... //operations Element&lt;T&gt;&amp; plus(const Element&lt;T&gt;&amp; v) const; Element&lt;T&gt;&amp; minus(const Element&lt;T&gt;&amp; v) const; ... }; //sum template &lt;class T&gt; Element&lt;T&gt;&amp; Vector&lt;T&gt;::plus(const Element&lt;T&gt;&amp; v) const { Element&lt;T&gt;* ret = new Vector((x + v.x), (y + v.y), (z + v.z)); return *ret; } //difference template &lt;class T&gt; Element&lt;T&gt;&amp; Vector&lt;T&gt;::minus(const Element&lt;T&gt;&amp; v) const { Vector&lt;T&gt;* ret = new Vector((x - v.x), (y - v.y), (z - v.z)); return *ret; } </code></pre> <p>but I always get</p> <blockquote> <p>error: 'const class Element' has no member named 'x'</p> </blockquote> <p>So, can I define my virtual functions to take Vector&amp; as an argument instead, or is there a way for me to access the data members of Vector through a pointer to Element?</p> <p>I'm still fairly new to inheritance polymorphism, fyi.</p> <p>EDIT: Trying a static_cast (as suggested below) hasn't solved my problem – or I have the wrong syntax for it. I've tried a static cast in the two ways I could imagine, and now my error is > error: 'const class Element' has no member named 'x' </p> <p>My updated code is: //sum template Element&amp; Vector::plus(const Element&amp; v) const { Element* ret = static_cast*>(operator new((x + v.x), (y + v.y), (z + v.z))); return *ret; } </p> <pre><code>//difference template &lt;class T&gt; Element&lt;T&gt;&amp; Vector&lt;T&gt;::minus(const Element&lt;T&gt;&amp; v) const { Element&lt;T&gt;* ret = new Vector&lt;T&gt;(static_cast&lt;Vector*&gt;((x - v.x), (y - v.y), (z - v.z))); return *ret; } </code></pre> <p>I thought the whole point of inheritance polymorphism is that a reference to a derived class is practically the same as a reference to a base class. Why do I have to jump through these hoops?</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.
 

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