Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Inheritance and how to pass and maintain subclass data through a superclass
    primarykey
    data
    text
    <p>Alright, wasn't quite sure how to word the question and couldn't find any duplicates that I think really address this situation.</p> <p>Essentially I have a super class that gets extra data appended to it through a subclass. The container class for this data recognizes only the super class and adjust characteristics based on an id parameter in the super class.</p> <p>I've actually never had to used inheritance in c++ till recently so forgive me if this is trivial. I'm under the impression that when I go to hard copy a bunch of data using the superclass, the subclass data is loss in translation so to speak. In order to bypass this limitation I'm trying to use a typecast-ed pointer however I now get a segmentation fault when trying to free the memory even when typecasting the pointer parameter in the free() function.</p> <p>Here is the sample code...</p> <p>Structs</p> <pre><code>// Super class struct Vertex { __declspec(align(4)) unsigned int vType; // Identifies the vertex type. Vertex(const unsigned int _vType) : vType(_vType) { } Vertex(const Vertex &amp;_rV) : vType(_rV.vType) { } // Copy constructor virtual ~Vertex() { } unsigned int GetVType() const { return vType; } }; // Subclass // Id = 1 struct V_Pos : Vertex { __declspec(align(4)) XMFLOAT3 position; V_Pos(void) : Vertex(1) { } V_Pos(XMFLOAT3 &amp;_rPosition) : Vertex(1), position(_rPosition) { } V_Pos(const V_Pos &amp;_rV) : Vertex(_rV), position(_rV.GetPosition()) { } // Copy constructor ~V_Pos() { } XMFLOAT3 GetPosition() const { return position; } }; </code></pre> <p>Here is how I'm currently copying the data.</p> <pre><code>// pBuffer is declared as a Vertex* data type pBuffer = new V_Pos[_bufSize]; if (_pVBuffer-&gt;GetVType() == 1) for (unsigned int i = 0; i &lt; bufSize; ++i) { V_Pos *_temp = (V_Pos*)&amp;_pVBuffer[i]; pBuffer[i] = *_temp; } </code></pre> <p>Here is how I am currently de-allocating the data.</p> <pre><code>if (pBuffer != 0) { delete [] pBuffer; pBuffer = 0; } </code></pre> <p>What is the correct approach for this situation?</p> <p><strong>Edit 1 -</strong></p> <p>Updated the above code blocks to clarify the comment discussion under knulp's answer.</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