开发者

C++ Inheritance and how to pass and maintain subclass data through a superclass

开发者 https://www.devze.com 2023-03-20 15:22 出处:网络
Alright, wasn\'t quite sure how to word the question and couldn\'t find any duplicates that I think really address this situation.

Alright, wasn't quite sure how to word the question and couldn't find any duplicates that I think really address this situation.

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.

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.

Here is the sample code...

Structs

// Super class
struct Vertex {
    __declspec(align(4)) unsigned int vType;              // Identifies the vertex type.

    Vertex(const unsigned int _vType) : vType(_vType) { }
    Vertex(const Vertex &_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 &_rPosition) : Vertex(1), po开发者_JS百科sition(_rPosition) { }
    V_Pos(const V_Pos &_rV) : Vertex(_rV), position(_rV.GetPosition()) { } // Copy constructor
    ~V_Pos() { }

    XMFLOAT3 GetPosition() const { return position; }
};

Here is how I'm currently copying the data.

// pBuffer is declared as a Vertex* data type
pBuffer = new V_Pos[_bufSize];

if (_pVBuffer->GetVType() == 1)
    for (unsigned int i = 0; i < bufSize; ++i) {
        V_Pos *_temp = (V_Pos*)&_pVBuffer[i];
        pBuffer[i] = *_temp;
    }

Here is how I am currently de-allocating the data.

if (pBuffer != 0) {
    delete [] pBuffer;
    pBuffer = 0;
}

What is the correct approach for this situation?

Edit 1 -

Updated the above code blocks to clarify the comment discussion under knulp's answer.


If you start mixing low level memory allocation with malloc()/free(), and C++ objects, you will run into a lot of troubles, while making your code almost unreadable.

You should create a new object with new on a proper constructor, which automatically 1) allocates memory and 2) initializes the struct. To properly free the memory you should use delete and the destructor.

You should copy using a copy constructor and an assignment operator. If you do not define them, the default ones are automatically defined by the compiler to perform a bitwise copy.

Why are you using a type field? C++ has a very strong typing features, so it makes very little sense to bypass all C++ mechanism to define a vType. Rather, define a base class, and two or more derived classes from there, and just eliminate the vtype field.

If you use clean OO programming, you will avoid all these problems from the start.


Your base class needs to have a virtual destructor. This will allow you to safely delete a derived class with a base class pointer.


Not that! Use a copy constructor.

    // Super class
struct Vertex {
    __declspec(align(4)) unsigned int vType; // Identifies the vertex type.
    Vertex(const unsigned int _vType) : vType(_vType) { }

    unsigned int GetVType() const { return vType; }

    Vertex(const Vertex& v) : vType(v.vType) {}
};

// Subclass
// Id = 1
struct V_Pos : Vertex {
    __declspec(align(4)) XMFLOAT3 position;

    V_Pos(void) : Vertex(1) { }
    V_Pos(XMFLOAT3 &_rPosition) : Vertex(1), position(_rPosition) { }

    V_Pos(const V_Pos& v) : Vertex(v) { 
       position[0] = v.position[0];
       position[1] = v.position[1];
       position[2] = v.position[2];
    }
};

Better yet, use a vecotr instead of XMFLOAT3;

Create a copy on the heap: V_Pos original(...); V_Pos * copyPtr = new V_Pos(original);

0

精彩评论

暂无评论...
验证码 换一张
取 消