Xavier Jones
Xavier Jones

Reputation: 1

How to access member functions when using object pointers

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?

Upvotes: 0

Views: 59

Answers (2)

Sandeep Tadepalli
Sandeep Tadepalli

Reputation: 76

Both nPtr1 and nPtr2 are referring to the same instance.

   nPtr2 = nPtr1;

nPtr1 is pointing to n1, so nPtr2 is now also pointing to n1. This is why you are getting the same value.

As to accessing the member functions in the correct way.

  nPtr2->GetValue();

The above is the right way to access member functions.

  *nPtr2->GetValue()
  *nPtr2.GetValue()

Both are wrong. To understand why, you need to look at the operator precedence.

In the first way i.e *nPtr2->GetValue(), the '->' operator has higher precedence so, it is similar to this

            *(nPtr2->GetValue()) 

This means you are trying to dereference (*) the return value of GetValue() which is a bool.

In the second way i.e *nPtr2.GetValue(), the '.' operator has higher precedence so, it is similar to writing

            *(nPtr2.GetValue()) 

Since nPtr2 is a pointer, you cannot use '.' on it.

You can read more about operator precedence here (https://en.cppreference.com/w/cpp/language/operator_precedence)

Upvotes: 0

Pickle Rick
Pickle Rick

Reputation: 808

Node *nPtr1 = &n1;
Node *nPtr2 = nPtr1;

Of course you're getting the same result when logging the m_value field. :) You can even see in your log they both point to the same instance.

Upvotes: 2

Related Questions