I am trying to override a function but end up with a segmentation fault. I followed some Tutorials and now I'am not able to find the origin of the segmentation fault.
I use 1 header and 1 cpp file. Update The Reason is that I want to Make multiple Command like print and instatiate different commands and call the Execute method without knowing what exactly command this is.
This is declared in one header File :
class Command{
public:
virtual int Execute(std::stack<NumericData>* stack)=0;
};
class Print : public Command{
public:
int Execute(std::stack<NumericData>* stack);
};
This is the implementation one cpp file:
... // inside some function
std::stack<NumericData> stack;
Command* command;
if(1){ // if is updated
Print print; // and reason for seg fault
command=&print; // without if it works
}
command->Execute(&stack); // <- segmentation fault
...
int Command::Execute(std::stack<NumericData>* stack){
printf("Execute parent\n");
return 0;
}
int Print::Execute(std::stack<NumericData>* stack){
printf("Execute child\n");
return 1;
}