overloaded assignment operator

closed account (j3bk4iN6)
1
2
3
4
5
6
7
8
9
10
11
12
13
//Overloaded assignment operator for CMessage objects
CMessage& operator=(const CMessage& aMess)
{
	//Release memory of 1st operand
	delete[] pmessage;
	pmessage = new char[ strlen(aMess.pmessage) + 1];

	//Copy 2nd operand string to 1st
	strcpy(this->pmessage, aMess.pmessage);

	//Return a reference to 1st operand
	return *this
}


I would like to know why you return a reference from the assignment operator function. The function does complete the assignment operation, and the object on the right of assignment will be copied to the left. It has something to do with foo.operator=(baz.operator=(bar)) so I think you have to at least return a CMessage object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

class Foo
{
public:
    Foo(){std::cout << "Contructor\n";}
    ~Foo(){std::cout << "Destructor\n";}
    Foo(const Foo&){std::cout << "copy constructor\n";}
    Foo& operator=(const Foo&){std::cout<< "operator=\n";return *this;}
};

int main()
{
    Foo obj1;
    Foo obj2(obj1);
    obj2 = obj1;

    return 0;
}

Output
Contructor
copy constructor
operator=
Destructor
Destructor

And now you suggestion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

class Foo
{
public:
    Foo(){std::cout << "Contructor\n";}
    ~Foo(){std::cout << "Destructor\n";}
    Foo(const Foo&){std::cout << "copy constructor\n";}
    Foo operator=(const Foo&){std::cout<< "operator=\n";return *this;}
};

int main()
{
    Foo obj1;
    Foo obj2(obj1);
    obj2 = obj1;

    return 0;
}

Output
Contructor
copy constructor
operator=
copy constructor
Destructor
Destructor
Destructor

Do you see that in you case new temporary variable is created on stack?
Last edited on
closed account (j3bk4iN6)
If the return type is just CMessage, it will not be legal because a temporary copy of the original object is actually returned, and the compiler will not allow a member function call using a temporary object. CMessage is not an lvalue. So I think the only way to get this to compile is return it as a reference.
I've provided compilable code.
And if you see the temporary object was created by copy contructor which was called by operator=.
If you returns by reference copy contructor will not be called.

And there is no problem with life time in both cases.
closed account (j3bk4iN6)
I mean in this instance (foo.operator=(bazz)).operator=(bar);
explicit overload.
Last edited on
I hoped that you will see the difference.

Both operator= are valid, but if you return by value then temporary object is created. And in your case it's no matter how to return.
closed account (j3bk4iN6)
I need an if statement for when I make a mistake and have something like foo = *fooPtr.
Thanks for your time, I guess the question was really, "Does it need to return anything?"
Superficially the object on the right will be copied to the object on the left. I guess it depends on how it is used.
I see the difference now that I looked at the outputs.
Last edited on
By reference case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

class Foo
{
public:
    Foo(){std::cout << "Contructor\n";}
    ~Foo(){std::cout << "Destructor\n";}
    Foo(const Foo&){std::cout << "copy constructor\n";}
    Foo& operator=(const Foo&){std::cout<< "operator=\n";return *this;}
    void testMe()const {std::cout << "test\n";}
};

int main()
{
    Foo obj1;
    Foo obj2(obj1);
    obj2 = obj1;
    Foo* ptr = &obj1;
    obj2 = *ptr;
    obj2.testMe();
    return 0;
}


Contructor
copy constructor
operator=
operator=
test
Destructor
Destructor

By value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

class Foo
{
public:
    Foo(){std::cout << "Contructor\n";}
    ~Foo(){std::cout << "Destructor\n";}
    Foo(const Foo&){std::cout << "copy constructor\n";}
    Foo operator=(const Foo&){std::cout<< "operator=\n";return *this;}
    void testMe()const {std::cout << "test\n";}
};

int main()
{
    Foo obj1;
    Foo obj2(obj1);
    obj2 = obj1;
    Foo* ptr = &obj1;
    obj2 = *ptr;
    obj2.testMe();
    return 0;
}


Contructor
copy constructor
operator=
copy constructor
Destructor
operator=
copy constructor
Destructor
test
Destructor
Destructor
closed account (j3bk4iN6)
Thanks, its clearer to me now.
Topic archived. No new replies allowed.