SUN Siqi
SUN Siqi

Reputation: 15

Ambiguous call to class member's reference

Say if I have a binary tree node defined like this: (and just assume all members are public for convenience...)

    class node
    {
    public:
       int v;
       node* left;
       node* right;
    //member functions....
    }

And I have two node pointers named A and B and run the following script:

    node* A;
    node* B;
    A->left = B;
    A->left = NULL;

Does that make B point to NULL or it only changes A's left child pointer (i.e. this->left in *A) and B still remains what it used to point to?

If the former case is true, how can I change A->left's value without affecting B?

TIA!

Upvotes: 0

Views: 63

Answers (1)

FreeNickname
FreeNickname

Reputation: 7764

No, it doesn't. A->left = NULL only changes the value of A->left.

Note: I'm assuming that A and B has been initialized somehow, because if you run

node* A; //uninitialized
node* B; //uninitialized
A->left = B;
A->left = NULL;

you'll get a crash.

Update/Clarification: Most likely, I mean. But even if it will work (for instance, due to your phenomenal (lack of) luck you might get a garbage value pointing to a valid memory region), it won't do anything good. If you'll get a valid memory region, and writing there will bring some desirable result, stop debugging immediately, and go to a nearby casino, you're having a lucky day :)

Upvotes: 3

Related Questions