Hi,
I have two classes, one derives from the other.
Object00.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#pragma once
#include <string>
#include <vector>
using namespace std;
class Object00
{
protected:
int index, status;
string name;
vector <string> vectorAdditionalMessages;
public:
Object00();
~Object00();
}
|
Object01.h
1 2 3 4 5 6 7 8 9 10
|
#pragma once
#include "Object00.h"
class CaveObject01 : public Object00
{
vector<string> vectorStatusMessages;
public:
Object01();
~Object01();
};
|
in my code I have a vector of Object00 to which I add objects of both Object00 and Object01
1 2 3 4 5 6 7 8 9 10 11
|
vector<Object00> vectorObjects;
// Object00 object1 is a correctly and fully initialized Object00
// Object00 object2 is a correctly and fully initialized Object00
// Object01 object3 is a correctly and fully initialized Object01
// Object01 object4 is a correctly and fully initialized Object01
vectorObjects.push_back(object1);
vectorObjects.push_back(object2);
vectorObjects.push_back(object3);
vectorObjects.push_back(object4);
|
So far so good.
I can look at the vectorObjects elements while debugging and see each element and its values; of course for object3 and object4 I can only see the members they inherit from Object00.
Now, I access an element from the vector
|
Object00 object = vectorObjects.at(2);
|
and I get object3. Again, I can only see the members it inherits from Object00.
So I want to cast object back to an Object01 so I can see its Object01 member.
After some searching, I think I need to do the following:
1 2 3 4
|
Object00 object = vectorObjects.at(2);
Object00 *pObject = &object;
Object01 *pObject01 = dynamic_cast<Object00*>(pObject);
|
Now, I get a complaint that
"the operand of a runtime dynamic_cast must have a polymorphic class type"
and found a couple of pages that said I needed a virtual destructor or function
so I put virtual in front of the Object00 destructor and now it runs but pObject01 shows no data whatsoever after the cast.
What's going on? Please be detailed in the code you give me.
If I do a static_cast (removing the virtual modifier), pObject01 shows the Object00 members' data and the Object01 members but not their data.