My classes have structure:
class Base
{
protected:
int size;
public:
Base(const Base &b); // copy constructor.
}
;
class Derived_1 : public Base
{
private:
double phase; // only Derived_1 has this private attribute.
public:
Derived_1(int size_, double phase_)
}
class Derived_2 : public Base {};
class Derived_3 : public Base {};
I use std::pair
to store the objects associated with another number.
std::vector<std::pair<int, Base>> num_base_list;
num_base_list.push_back({1, Derived_1{10, 90.0}});
How do I get the phase
attribute from num_base
?
I tried to use dynamic_cast
Derived_1* derived_1 = dynamic_cast<Derived_1*>(&base);
// base is one of the Base object in the num_base_list
but received this error:the operand of a runtime dynamic_cast must have a polymorphic class typeC/C++(698)
.