A question about 'Return Value'

This is a discussion on A question about 'Return Value' within the C++ Programming forums, part of the General Programming Boards category; I have the following code just for fun Code: class Base{ public: Base(){cout<<"Constructor"<<endl;} Base(const Base &obj){cout<<"Copy Constructor"<<endl;} Base & operator=(const ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A question about 'Return Value'

    I have the following code just for fun

    Code:
    class Base{
    public:
    	Base(){cout<<"Constructor"<<endl;}
    	Base(const Base &obj){cout<<"Copy Constructor"<<endl;}
    	Base & operator=(const Base& obj){
    		cout<<"Operator="<<endl;
    		return *this;
    	}
    };
    
    Base foo(){
    	Base x;
    	return x;
    }
    
    void main(){
    	Base tmp = foo();
    }
    =================
    Why the outputs are only

    Constructor
    Copy Constructor

    But I think the outputs should be:
    Constructor
    Copy Constructor // from return x
    Copy Constructor // from Base tmp = foo();
    Last edited by meili100; 06-04-2007 at 12:44 AM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    tmp called the copy constructor automatically. Nah nah.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    But I think the outputs should be:
    Constructor
    Copy Constructor // from return x
    Copy Constructor // from Base tmp = foo();

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Your compiler is probably optimizing out one of the copies.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    This:
    Code:
    Base tmp = foo();
    Is rearranged by the compiler to become:
    Code:
    Base tmp(foo());
    So, the constructor is called in foo, and then this object is used (copy constructor called) to initialize tmp, so the output is as you've mentioned:
    Constructor (from inside foo function)
    Copy Constructor (initializing tmp from foo's return value)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And a compiler would be allowed to be even more aggressive and eliminate even the single copy being done.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Side note: void main() is evil. Use int main().

  8. #8
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    u have a mistake in the assignment operator, you should check if the passed object is the same one (memory address)
    Code:
    Base & operator=(const Base& obj){
                   //Check if the object is being assigned to it self
                   //By comparing the memory adress
                   if(this == &obj) {return *this;}
    		cout<<"Operator="<<endl;
    		return *this;
    	}
    Side note: void main() is evil. Use int main().
    Exactly , Bjarne in his website wrote: It has never been "void" for neither C++ nor C
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  9. #9
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by Hussain Hani View Post
    u have a mistake in the assignment operator, you should check if the passed object is the same one (memory address)
    It's actually pretty wasteful to check for self assignment in cases where it is harmless, such as this case.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by AverageSoftware View Post
    It's actually pretty wasteful to check for self assignment in cases where it is harmless, such as this case.
    Indeed.
    This is a good read on the subject of self-assignment:
    http://www.gotw.ca/gotw/011.htm
    Last edited by iMalc; 06-05-2007 at 02:15 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Union Question
    By estarapapax in forum C Programming
    Replies: 0
    Last Post: 10-21-2008, 11:08 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21