Note that there are some explanatory texts on larger screens.

plurals
  1. POPolymorphism/inheritance issue with virtual class member function
    primarykey
    data
    text
    <p>I might have the wrong idea on exactly what polymorphism versus inheritance is, but basically what I'm trying to do is have <strong><em>classB</em></strong> derive from <strong><em>classA</em></strong>, and create a <strong><em>classB</em></strong> which overrides a pure virtual member function of <strong><em>classA</em></strong>, like so:</p> <p><br/> <strong>classA:</strong></p> <pre><code> ///////////////// // CodeBlock.h // ///////////////// typedef enum { CCBT_UNDEFINED, CCBT_FUNCTION, //... } CODE_BLOCK_TYPE; class CCodeBlock { public: CCodeBlock::CCodeBlock(); CCodeBlock::CCodeBlock(CString&amp; source, DWORD startPos); CCodeBlock::~CCodeBlock(); virtual CODE_BLOCK_TYPE CCodeBlock::GetType() = 0 CString m_code; DWORD m_startPos; DWORD m_length; int m_numLines; } /////////////////// // CodeBlock.cpp // /////////////////// //... CCodeBlock::CCodeBlock(CString&amp; source, DWORD startPos) : m_code(source), m_startPos(startPos) { m_length = m_code.GetLength(); } CODE_BLOCK_TYPE CCodeBlock::GetType() { return CCBT_UNDEFINED; } </code></pre> <p><br/> <strong>classB:</strong></p> <pre><code> ///////////////////// // FunctionBlock.h // ///////////////////// #include "CodeBlock.h" class CFunctionBlock : public CCodeBlock { public: CFunctionBlock::CFunctionBlock(); CFunctionBlock::CFunctionBlock(CString&amp; source, DWORD startPos); CFunctionBlock::~CFunctionBlock(); CODE_BLOCK_TYPE CFunctionBlock::GetType(); } /////////////////////// // FunctionBlock.cpp // /////////////////////// //... CFunctionBlock::CFunctionBlock(CString&amp; source, DWORD startPos) { m_code = source; m_startPos = startPos; } CFunctionBlock::~CFunctionBlock() { CCodeBlock::~CCodeBlock(); } CODE_BLOCK_TYPE CFunctionBlock::GetType() { ////////////////////////////// // &gt;&gt; NEVER GETS CALLED! &lt;&lt; // ////////////////////////////// return CCBT_FUNCTION; } </code></pre> <p><br/> <strong>main:</strong></p> <pre><code>CCodeBlock *block = new CFunctionBlock(L"function hello(){ print('hello') }", iPos) CODE_BLOCK_TYPE type = block-&gt;GetType(); // ALWAYS RETURNS CCBT_UNDEFINED! </code></pre> <p><br/> As you can see, <strong><em>GetType()</em></strong> always returns <I>CCBT_UNDEFINED</I>. Keep in mind that CCodeBlock is meant to be a 'generic' version of CFunctionBlock (as well as a few other like-minded classes, some of which contain a CCodeBlock 'm_parent' member variable), and is supposed to inherit any CCodeBlock member variables &amp; member functions, as well as override a specific list of functions contained in CCodeBlock.</p> <p>How can this be achieved? Would I need to resort to using templates (if that's even possible)?</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.
 

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