I set up a "parent class pointer" vector with size 2 to store its "derived class" address but seem like this vector only stores final address no matter how many address i added, so its cause "Access violation reading" problem. By the way i used composite pattern.
I've tried many ways, i've used vector, 2d pointer (Ex: int**).
class Node{
public:
virtual double evaluate();
};
class NumNode: public Node{
private:
double number;
public:
double evaluate();
NumNode(int);
};
class OpNode: public Node{
private:
char operation;
vector<Node*> branch;
public:
double evaluate();
void addLeft(NumNode);
void addRight(NumNode);
OpNode(char);
};
double OpNode::evaluate(){
if(this->operation == '+')
return this->branch[0]->evaluate() + this->branch[1]-
>evaluate();**Exception thrown at 0x008C6097 in
Project_Testing.exe: 0xC0000005: Access
violation reading location 0xCCCCCCCC.**
return 0;
}
void OpNode::addLeft(NumNode other){this->branch[0] = &other;}
void OpNode::addRight(NumNode other){this->branch[1] = &other;}
int main(){
OpNode n('+');
n.addLeft(NumNode(2));
n.addRight(NumNode(3));
cout << n.evaluate() << endl;
}
As you look into the main funciton, i've add two diffrent NumNode. But when i started debugging.
branch[0] 0x006ff620 {...} Node *
branch[1] 0x006ff620 {...} Node *.
They had the same address!