Need to understand the overloaded assignment operator behavior


 
Thread Tools Search this Thread
Top Forums Programming Need to understand the overloaded assignment operator behavior
# 1  
Old 03-07-2013
Need to understand the overloaded assignment operator behavior

Hi,

In the following code,
Code:
class A {
public:
    void operator=(const A& rhs) {
        if (this == &rhs) cout << "self-assigned";
    }
};

class B {
    A a; // should not be a pointer member, (i.e) A* a
};

int main() {
    B b;
    b = b;  // Ans: self-assigned
}

I am really not getting the logic, why the class A's overloaded assignment operator is called when object B is self assigned? Could anyone help me in understanding this behavior?

Last edited by royalibrahim; 03-07-2013 at 02:41 AM..
# 2  
Old 03-07-2013
You're not overloading anything. Your class B contains an object of type A, is all. A's own member functions are called to handle and copy objects of type A.

If the B class was descended from the A class, that would be overloading, if you overloaded its copy constructor.

Code:
#include <iostream>
using namespace std;

class A {
public:
        void operator=(const A &rhs) {
                if(this == &rhs) cout <<"self-assigned\n";
        }
};

class B:public A {
public:
        void operator=(const B &rhs) {  cout <<"overloaded\n";  }
};

int main(void)
{
        B a;
        a=a;
}

Here, class B is derived from class A, and provides an alternative copy constructor, which gets used instead of A's.
# 3  
Old 03-08-2013
Quote:
Originally Posted by Corona688
If the B class was descended from the A class, that would be overloading, if you overloaded its copy constructor.
@Corona688, thank you for the reply. But even in the case when class B is not derived/inherited from A, and having the custom assignment operator implemented (like in the below code), then the B's overloaded assignment (only) is called.
Code:
class A {
public:
    void operator=(const A& rhs) {
        if (this == &rhs) cout << "self-assigned";
    }
};

class B {
    A a; // should not be a pointer member, (i.e) A* a
public:
    void operator=(const B& rhs) {
        cout << "B's" << endl;
    }
};

int main() {
    B b;
    b = b;  // Ans: B's
}

my understanding was the user implemented overloaded assignment operator function will be invoked at the time when that class's object is assigned. If there is no such implementation in that class then the compiler provided default assignment operator will be called, theoretically. Also, in class B there is just a containment member object and my doubt is why the class A's assignment operator is invoked when instance of B is assigned? Is this a compiler's optimization technique?

Last edited by royalibrahim; 03-08-2013 at 06:49 AM..
# 4  
Old 03-08-2013
Quote:
Originally Posted by royalibrahim
@Corona688, thank you for the reply. But even in the case when class B is not derived/inherited from A, and having the custom assignment operator implemented (like in the below code), then the B's overloaded assignment (only) is called.
All your operator= does is this:

Code:
cout << "B's" << endl;

No more, no less.

Overloading the default, means the default isn't called.

Quote:
my understanding was the user implemented overloaded assignment operator function will be invoked at the time when that class's object is assigned.
operator= is the thing which actually does assignment. Imagine the default as something like this:

Code:
void operator=(const B &rhs)
{
        a=rhs.a;
}

When you overloaded it, you told it to do this instead:

Code:
cout << "B's" << endl;

# 5  
Old 03-15-2013
I understand your point Corona688, but I am still unable to figure out the answer to my question Smilie, that is why the class A's assignment operator is invoked when instance of B is assigned when class A and B has no inheritance relation between them except containment object?

Also, I noticed that suppose if there is any other containment object, say "C c" data member available inside class B and if class C is having an assignment operator overloaded function implemented then during the b = b; both A's and C's version of assignment operator functions will be called according to the order of data member in class B. So is there a rule like, when there is a container object then that class's overloaded assignment operator function should be invoked when there is none present in the calling class's object?

Last edited by royalibrahim; 03-15-2013 at 07:56 AM..
# 6  
Old 03-15-2013
Quote:
Originally Posted by royalibrahim
I understand your point Corona688, but I am still unable to figure out the answer to my question Smilie, that is why the class A's assignment operator is invoked when instance of B is assigned when class A and B has no inheritance relation between them except containment object?
What method would you expect it to use, to overwrite B's contents of type A?

A's own operators are the only way the compiler has to do so. Even a default operator is still an actual function that gets called.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

2. Shell Programming and Scripting

Assignment operator without operand

Does anyone know how this line in bash works? local gotbase= force= nicelevel corelimit local pid base= user= nice= bg= pid_file= local cgroup= These lines are part of the daemon function inside the "functions" file at /etc/init.d in RH. (3 Replies)
Discussion started by: Rameshck
3 Replies

3. UNIX for Dummies Questions & Answers

Need a little help with assignment

Hello all im currently working on this assignment and a little stump on how to check for an argument heres the instructions: Step 4: Modify your script so that if there is an argument called TestError you display the following error message with your usage statement. TestError found Example:... (1 Reply)
Discussion started by: bsn3971
1 Replies

4. Solaris

Looking to understand what this command $$ does in variable assignment?

Hi Folks, I'm looking to figure something out in an existing script I'm trying to understand. the command in question(on a Solaris Box using KSH) is: WORKDIR=/tmp/namereplaced.exec.$$.$RANDOM Now, I know it's setting the $workdir environmental variable... And I understand most of... (2 Replies)
Discussion started by: Marc G
2 Replies

5. Homework & Coursework Questions

Assignment Help

1. List commands to create the directory hierarchy $HOME/a/b/c in vi to replace all occurences of TMP with tmp in lines 1 through 10 in vi to replace first occurence of CPU_file with DISK_file at line 15 2. Explain with a very simple example, usage of "ls -a" 3. What do the... (2 Replies)
Discussion started by: jessesaini
2 Replies

6. Programming

c++ assignment operator overloading

Hello everyone! Suppose that I have something like this A a; a.mem=new int; A b = a; where class A { public: int * mem; A() : mem(NULL) { } ~A() { if (mem!=NULL) delete mem; (1 Reply)
Discussion started by: bashuser2
1 Replies

7. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

8. Shell Programming and Scripting

scalar variable assignment in perl + { operator

When reading over some perl code in a software document, I came across an assignment statement like this $PATH = ${PROJECT}/......./.... In this particular form of scalar variable assignment, what does the curly braces operators do ? Also, what is the benefit in doing scalar assignment this... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Programming

what is diff b/w copy constructor and overloaded assignment operator

Helo i m new in c++. i m confuse about what is exact difference b/w copy constructor and overloaded assignment operator. Regards, Amit (3 Replies)
Discussion started by: amitpansuria
3 Replies
Login or Register to Ask a Question