assignment operator overloading

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class ABC{
int i;

public:
    ABC(int ii):i(ii){}

	// copy-ctor
    ABC(const ABC& obj)
    {
        i+=obj.i;
    }

    // assignment operator
    ABC operator=(const ABC& obj)
    {
        i+=obj.i;
        return *this;
    }

    int getvalue()
    {
    	return i;
    }

    void modify()
    {
        i+=2;
    }
};

int main()
{
    ABC p(10);
    ABC q(80);
    ABC r(90);

    ABC s = (p=q);
    s.modify();

   cout<<s.getvalue()<<endl;
   cout<<p.getvalue()<<endl;
}

output :

garbage
90


Q1: Why is it so?

I understand that because of ABC operator=(const ABC& obj), a temporary object will be returned with value of q.i . BUT that's not looking to be the case. Even p is not getting the correct value.

Q2: What is the reason for/ when to return by reference? ABC&operator=(const ABC& obj).

EDIT 1 :
corrected code and made more understandable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class ABC{
int i;

public:
	ABC(int ii):i(ii){}

	// copy-ctor
    ABC(const ABC& obj)
    {
        i=obj.i;
    }

    // assignment operator
	ABC operator=(const ABC& obj)
    {
        i=obj.i;
        return *this;
    }

    int getvalue()
    {
    	return i;
    }

    void modify()
    {
        i+=2;
    }
};

int main()
{
    ABC i(10);
    ABC j(80);
    ABC k(90);

    (i=j).modify();
    ((i=j)=k);
}


Q: the code doesn't give compile error. By returning by value ,compiler creates temporary object, which is const, so why does (i=j).modify(); work?
Also, ((i=j)=k); should not support the chaining.
Why is it so?
Last edited on
`foo += bar' means `foo = foo+bar'
you've got garbage because you are using an uninitialized variable in the copy constructor
you've got 90 because 80+10=90


> What is the reason for/ when to return by reference?
http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html
If it's good enough for ints, it's good enough for user-defined data-types.
@ne55 please see the edit in my post. I have 1 more question.
By returning by value ,compiler creates temporary object, which is const
Where? If you want returned object to be const, you need to mark it so: const ABC operator=(const ABC& obj) or mark modify as only executable on l-values: void modify() &
> why does (i=j).modify(); work?
> Also, ((i=j)=k); should not support the chaining.
> Why is it so?

modify() and operator=() are member functions; an rvalue of (non-const) class type can be modified through a member function.

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

int foo() { return 100 ; }
const int const_foo() { return 100 ; } // const qualifier on prvalue of non-class type is ignored

std::string bar() { return "abc" ; }
const std::string const_bar() { return "abc" ; } // rvalues of class-type may be cv-qualified

void baz( std::string& str ) { str += "def" ; }

int main()
{
    foo() += 2 ; // *** error: rvalue of non-class type can't be modified

    bar() += "def" ; // fine: rvalue of (non-const) class type can be modified through a member function

    baz( bar() ) ; // *** error: rvalue can't be used to initialise reference to non-const

    const_bar() += "def" ; // *** error: rvalue of const class type can't be modified
}
Topic archived. No new replies allowed.