Can operator = not be a member?

Having a design in the form of:

struct Node
{
Node():left_(nullptr), right_(nullptr)
{ }
int id_;
Node* left_;
Node* right_;
};

I would like to include the syntax:

Node parent;
Node child;
parent.right_ = child;

So for this I need:

Node& operator=(Node* left, Node right);

but I get msg that operator = should be a member of fnc; Is there a way around this limitation?

+3
source share
4 answers

Automatically converting from value to pointer creates many possibilities so that your code is hard to understand.

If you still want to do this, you can provide the conversion operator in the Node class:

operator Node* () { return this; }
+2
source

parent.right_ = &child? , , . , , , .

, , , , Boost.Format, , , , ( %). -, .

+5

=() . .

+4

parent.right_ = child;

parent.right_ = &child;

;

+2

Source: https://habr.com/ru/post/1739352/


All Articles