Iām trying to understand a C++ code that uses vector which stores unique_ptr<Base>
, where Base
is a base class and has a derived class Derivate
.
When pushing unique_ptr<Derivate>
into this vector, there is no errors and can correctly call the method of derived class.
But when trying to modify a specific attribute of Derivate
, there is an error " error: 'class Base' has no member named 'deri_name' ".
The code is as follow:
#include<iostream>
#include<vector>
#include <memory>
using namespace std;
class Base
{
public:
virtual void test(){
cout << "this is Base test" << endl;
}
};
class Derivate :public Base
{
public:
Derivate(const string& name):deri_name(name){
}
virtual void test(){
cout << "this is Derivate test by " << deri_name << endl;
}
string deri_name;
};
int main()
{
vector<unique_ptr<Base>> vec;
vec.push_back(make_unique<Derivate>("wayne"));
vec[0]->test(); // will sprint "this is Derivate test by wayne"
//vec[0]->deri_name = 'wong'; // will report an error " error: 'class Base' has no member named 'deri_name' "
return 0;
}
I've tried some methods, but there seems no straightforward way to cast vec[0]
from unique_ptr<Base>
to unique_ptr<Derivate>
could I possibly modify vec[0]->deri_name
without modifying the type of vec
?