1

I have the following class declaration:

Class A { A &operator =(const ACHAR *s); // (1) A &operator =(A &a); // (2) A operator +(const ACHAR *s); // (3) operator ACHAR*(); // (4) }; 

Now, when I run this code:

A x,y; ACHAR *p; x=y; // (2) x=y+p; (3)(4)(1) 

The first assignment x=y invoke the (2) operator=, as expected.

But the second assignment provoke the cast operator, and the (1) operator=.

Now, since the (3) operator+ returns class A, I would expect (2) operator= to be called.

Why is this happen, and how to resolve *this?

Thanks in advance!

1 Answer 1

2
A& operator=(A& a); 

it should be:

A& operator=(const A &a); ^^^^^ 

In your case y + p produces an rvalue of type A which cannot be captured by non-const reference A&, so 4 and 1 are called instead of 2.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.