class SymbolNode {
public:
string name;
Type type;
int offset;
SymbolNode(string name_, Type type_, int offset_): name(name_), type(type_), offset(offset_) {
}
};
class FuncNode : public SymbolNode {
public:
Type returnType;
vector<Type> entries;
FuncNode(string name_, Type type_, int offset_,Type returnType,vector<Type> entries):
SymbolNode(name_,type_,offset_) ,returnType(returnType),entries(entries) {}
};
So I have the inherited class FuncNode of the base class SymbolNode. And when I try to access the elements of func I can't access all of them. I would like to add func to vector Symbols . But also have the ability to access all the elements.
std::shared_ptr<SymbolNode> func= make_shared<FuncNode>("inc",FUNC,1,INT,entries);
vector<std::shared_ptr<SymbolNode>> Symbols;
Try dynamic_cast or static_cast
std::shared_ptr<SymbolNode> func= make_shared<FuncNode>("inc",FUNC,1,INT,entries);
IN the above line, func
is a (shared) pointer to the base class. As such, it doesn't know what it's derived type this. Hence, it can't access the members of FuncNode
You can express it as this:
std::shared_ptr<FuncNode> func= make_shared<FuncNode>("inc",FUNC,1,INT,entries);
Or do this:
std::shared_ptr<SymbolNode> symnode = make_shared<FuncNode>("inc",FUNC,1,INT,entries);
std::shared_ptr<FuncNode> func = static_pointer_cast<FuncNode>(symnode);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.