I am trying to access the value Node through a nodePtr, but am having trouble.
//main//
Node n1 (true);
Node n2 (false);
Node *nPtr1 = &n1;
Node *nPtr2 = nPtr1;
cout << "Memory Locations: " << endl <<
"\tn1: " << &n1 << endl <<
"\tn2: " << &n2 << endl;
cout << "Pointer Info: " << endl <<
"\tnPtr1: " << endl <<
"\t\tMemory Address: " << &nPtr1 << endl <<
"\t\tPoints to: " << nPtr1 << endl <<
"\t\tValue of Pointee: " << nPtr1->GetValue() << endl <<
endl <<
"\tnPtr2: " << endl <<
"\t\tMemory Address: " << &nPtr2 << endl <<
"\t\tPoints to: " << nPtr2 << endl <<
"\t\tValue of Pointee: " << nPtr2->GetValue() << endl <<
endl;
//Node.cpp//
Node::Node(){
m_next = nullptr;
}
Node::Node(bool value){
m_value = value;
m_next = nullptr;
}
void Node::ReplaceValue(){
m_value = !m_value;
}
void Node::SetNext(Node* next){
m_next = next;
}
Node* Node::GetNext(){
return m_next;
}
bool Node::GetValue(){
return m_value;
}
Node::~Node(){
}
When I run this code I get this:
Memory Locations:
n1: 0x7ffd33aee010
n2: 0x7ffd33aee000
Pointer Info:
nPtr1:
Memory Address: 0x7ffd33aedfe8
Points to: 0x7ffd33aee010
Value of Pointee: 1
nPtr2:
Memory Address: 0x7ffd33aedfe0
Points to: 0x7ffd33aee010
Value of Pointee: 1
However the expected output of Value of Pointee should reflect the boolean that the Node was initialized with.
I have tried using *nPtr2->GetValue() as well as *nPtr2.GetValue(), but both of those result in syntax errors.
What is the correct way to access these member functions when doing so through pointers? And if I am accessing them correctly, then why do the values of the nodes not match the expected?