C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
= Operator and Copy CTOR Query Posted by shailendrab on 19 Sept 2006 at 8:19 AM
Can anyone provide me the answer of the following question :

In what situation, compiler checks first for assignment operator, if it is not implemented then checks for copy constructor and calls copy constructor?
Report
Re: = Operator and Copy CTOR Query Posted by bilderbikkel on 19 Sept 2006 at 8:49 AM
: Can anyone provide me the answer of the following question :
:
: In what situation, compiler checks first for assignment operator, if it is not implemented then checks for copy constructor and calls copy constructor?
:
If you call the assignment operator?
MyClass a;
MyClass b;
b = a; //Checks assignment operator, if not present, calls copy constructor


Or do I overlook something?
See ya,
bilderbikkel

Report
Re: = Operator and Copy CTOR Query Posted by shailendrab on 19 Sept 2006 at 11:24 PM
: : Can anyone provide me the answer of the following question :
: :
: : In what situation, compiler checks first for assignment operator, if it is not implemented then checks for copy constructor and calls copy constructor?
: :
: If you call the assignment operator?
:
: MyClass a;
: MyClass b;
: b = a; //Checks assignment operator, if not present, calls copy constructor
: 

:
: Or do I overlook something?
: See ya,
: bilderbikkel
:
:

Hi bilderbikkel,

Have you tested this code? I did, it doesn't work. actually theoretically Copy Constructor get called in three situation only,

1. when you create an object from another
2. you pass a object into function as parameter
3. you return an object from a function

if compiler search for CC if = operator not present and CC can used in place of = operator then we dont need to have = operator.... right?

I dont know whether it is possible, compiler call Copy Ctor if not found = operator.

If anyone say yes it happens then let me know the case.

what you think?

Report
Re: = Operator and Copy CTOR Query Posted by bilderbikkel on 20 Sept 2006 at 4:27 AM
You are totally right!

The code I suggested does not call the copy constructor:
: :
: : MyClass a;
: : MyClass b;
: : b = a;
: : 


But code like this will:
  MyClass a;
  MyClass b = a;


I pasted the code below if you want to check it:

#include <iostream>

//The Gossip class I use from www.codepedia.com/1/CppGossip
struct Gossip
{
  Gossip(const Gossip& g)
  {
    std::cout << "Copy constructor" << std::endl;
  }
  Gossip& Gossip::operator= (const Gossip& g)
  {
    if (this == &g)
    {
      std::cout << "Assigment operator: prevented self-assignment" << std::endl;
      return *this;
    }
    std::cout << "Assigment operator" << std::endl;
    return *this;
  }
};

//I commented the screen output
//generated by every scope
int main()
{
  {
    std::cout << "*** Test 1 ***" << std::endl;
    Gossip a;
    Gossip b = a;
    //Constructor
    //Copy constructor
    //Destructor
    //Destructor
  }
  {
    std::cout << "*** Test 2 ***" << std::endl;
    Gossip a;
    Gossip b;
    a = b;
    //Constructor
    //Constructor
    //Assignment operator
    //Destructor
    //Destructor
  }
}


Test #2 was the one I incorrectly suggested, test #1 does exactly what you requested. Note that it does call the copy constructor even when the assignment operator is defined!

See ya,
bilderbikkel

Report
Re: = Operator and Copy CTOR Query Posted by Lundin on 20 Sept 2006 at 4:44 AM
: You are totally right!
:
: The code I suggested does not call the copy constructor:
: : :
: : : MyClass a;
: : : MyClass b;
: : : b = a;
: : : 

:
: But code like this will:
:
:   MyClass a;
:   MyClass b = a;
: 

:
: I pasted the code below if you want to check it:
:
:
: #include <iostream>
: 
: //The Gossip class I use from www.codepedia.com/1/CppGossip
: struct Gossip
: {
:   Gossip(const Gossip& g)
:   {
:     std::cout << "Copy constructor" << std::endl;
:   }
:   Gossip& Gossip::operator= (const Gossip& g)
:   {
:     if (this == &g)
:     {
:       std::cout << "Assigment operator: prevented self-assignment" << std::endl;
:       return *this;
:     }
:     std::cout << "Assigment operator" << std::endl;
:     return *this;
:   }
: };
: 
: //I commented the screen output
: //generated by every scope
: int main()
: {
:   {
:     std::cout << "*** Test 1 ***" << std::endl;
:     Gossip a;
:     Gossip b = a;
:     //Constructor
:     //Copy constructor
:     //Destructor
:     //Destructor
:   }
:   {
:     std::cout << "*** Test 2 ***" << std::endl;
:     Gossip a;
:     Gossip b;
:     a = b;
:     //Constructor
:     //Constructor
:     //Assignment operator
:     //Destructor
:     //Destructor
:   }
: }
: 

:
: Test #2 was the one I incorrectly suggested, test #1 does exactly what you requested. Note that it does call the copy constructor even when the assignment operator is defined!
:
: See ya,
: bilderbikkel
:
:


The original question was

"In what situation, compiler checks first for assignment operator, if it is not implemented then checks for copy constructor and calls copy constructor?"

In your first example it will not check for the assignment operator, it will go straight for the copy constructor. Note that these two rows have exactly the same meaning:

MyClass x(y); // copy constr
MyClass x=y; // copy constr


If you removed the assignment operator in your second example, it wouldn't run the copy constructor - it would run the default assignment operator, meaning that it would copy all the data from y to x (and all possible pointers in y will be pointing at the wrong location).

Sidenote: A general rule is that if your class needs to implement a copy constructor, the assignment operator or a destructor, it will need to implement all 3 of them.

As for the OP's question, I can't think of any such situation.


Report
Re: = Operator and Copy CTOR Query Posted by bilderbikkel on 20 Sept 2006 at 4:55 AM
: The original question was
:
: "In what situation, compiler checks first for assignment operator, if it is not implemented then checks for copy constructor and calls copy constructor?"
:
: In your first example it will not check for the assignment operator, it will go straight for the copy constructor. Note that these two rows have exactly the same meaning:
:
: MyClass x(y); // copy constr
: MyClass x=y; // copy constr
:
:
: If you removed the assignment operator in your second example, it wouldn't run the copy constructor - it would run the default assignment operator, meaning that it would copy all the data from y to x (and all possible pointers in y will be pointing at the wrong location).
:
: Sidenote: A general rule is that if your class needs to implement a copy constructor, the assignment operator or a destructor, it will need to implement all 3 of them.
:
: As for the OP's question, I can't think of any such situation.
:
Geez Lundin, you are totally right! Sorry for being such a louzy question reader sometimes. Rereading the question and your suggestion, I don't recall a situation either...

See ya,

bilderbikkel

Report
Re: = Operator and Copy CTOR Query Posted by shailendrab on 20 Sept 2006 at 5:10 AM
: You are totally right!
:
: The code I suggested does not call the copy constructor:
: : :
: : : MyClass a;
: : : MyClass b;
: : : b = a;
: : : 

:
: But code like this will:
:
:   MyClass a;
:   MyClass b = a;
: 

:
: I pasted the code below if you want to check it:
:
:
: #include <iostream>
: 
: //The Gossip class I use from www.codepedia.com/1/CppGossip
: struct Gossip
: {
:   Gossip(const Gossip& g)
:   {
:     std::cout << "Copy constructor" << std::endl;
:   }
:   Gossip& Gossip::operator= (const Gossip& g)
:   {
:     if (this == &g)
:     {
:       std::cout << "Assigment operator: prevented self-assignment" << std::endl;
:       return *this;
:     }
:     std::cout << "Assigment operator" << std::endl;
:     return *this;
:   }
: };
: 
: //I commented the screen output
: //generated by every scope
: int main()
: {
:   {
:     std::cout << "*** Test 1 ***" << std::endl;
:     Gossip a;
:     Gossip b = a;
:     //Constructor
:     //Copy constructor
:     //Destructor
:     //Destructor
:   }
:   {
:     std::cout << "*** Test 2 ***" << std::endl;
:     Gossip a;
:     Gossip b;
:     a = b;
:     //Constructor
:     //Constructor
:     //Assignment operator
:     //Destructor
:     //Destructor
:   }
: }
: 

:
: Test #2 was the one I incorrectly suggested, test #1 does exactly what you requested. Note that it does call the copy constructor even when the assignment operator is defined!
:
: See ya,
: bilderbikkel
:
:

Lundin posted correctly what i was planning to write to you bilderbikkel.

Anyways thank to both of you for answering my question.

This question was asked to me in an interview, so i am not sure whether it is possible or not. This is something i never read or come across in the code but that doesn't mean it can not be possible.

So guys we need to keep asking this question until we are sure it is not possible.

Looking for more update on this... :)


Report
Re: = Operator and Copy CTOR Query Posted by Lundin on 20 Sept 2006 at 7:33 AM
: : You are totally right!
: :
: : The code I suggested does not call the copy constructor:
: : : :
: : : : MyClass a;
: : : : MyClass b;
: : : : b = a;
: : : : 

: :
: : But code like this will:
: :
: :   MyClass a;
: :   MyClass b = a;
: : 

: :
: : I pasted the code below if you want to check it:
: :
: :
: : #include <iostream>
: : 
: : //The Gossip class I use from www.codepedia.com/1/CppGossip
: : struct Gossip
: : {
: :   Gossip(const Gossip& g)
: :   {
: :     std::cout << "Copy constructor" << std::endl;
: :   }
: :   Gossip& Gossip::operator= (const Gossip& g)
: :   {
: :     if (this == &g)
: :     {
: :       std::cout << "Assigment operator: prevented self-assignment" << std::endl;
: :       return *this;
: :     }
: :     std::cout << "Assigment operator" << std::endl;
: :     return *this;
: :   }
: : };
: : 
: : //I commented the screen output
: : //generated by every scope
: : int main()
: : {
: :   {
: :     std::cout << "*** Test 1 ***" << std::endl;
: :     Gossip a;
: :     Gossip b = a;
: :     //Constructor
: :     //Copy constructor
: :     //Destructor
: :     //Destructor
: :   }
: :   {
: :     std::cout << "*** Test 2 ***" << std::endl;
: :     Gossip a;
: :     Gossip b;
: :     a = b;
: :     //Constructor
: :     //Constructor
: :     //Assignment operator
: :     //Destructor
: :     //Destructor
: :   }
: : }
: : 

: :
: : Test #2 was the one I incorrectly suggested, test #1 does exactly what you requested. Note that it does call the copy constructor even when the assignment operator is defined!
: :
: : See ya,
: : bilderbikkel
: :
: :
:
: Lundin posted correctly what i was planning to write to you bilderbikkel.
:
: Anyways thank to both of you for answering my question.
:
: This question was asked to me in an interview, so i am not sure whether it is possible or not. This is something i never read or come across in the code but that doesn't mean it can not be possible.
:
: So guys we need to keep asking this question until we are sure it is not possible.
:
: Looking for more update on this... :)
:
:
:


Heh, I'm sitting here, pondering about a few such questions since we are about to hire someone. Maybe I should ask this one just to be mean? Not that we are using C++ or anything, but still...



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.