Thread: Temporary object created by calling constructor

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    I don't get the last part of it
    <b>treating it as the same object as the parameter.</b>
    Do you mean that it looks at the overloaded constructor signature and therefore as a result may elide the use of the copy constructor depending on that.
    Well, it cannot elide a copy constructor invocation if the copy constructor is not to be invoked in the first place. So, it notes that pass by value is used, hence a copy constructor should be invoked. Now, based on other factors, e.g., the fact that the function call involves a temporary object being created and then immediately copied, it decides that the copying is actually unnecessary. Therefore, instead of copying the temporary object, it treats it as if the temporary object and the object in the function are one and the same. Thus, all it needs to do is generate the code to create the object (hence the Dog(int, int) constructor call), but can elide the code to copy it (hence no copy constructor call).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by laserlight View Post
    . Therefore, instead of copying the temporary object, it treats it as if the temporary object and the object in the function are one and the same.
    What function are we talking about here? to clarify do you mean the the coolFunction(....) function?
    em.
    if that is the case
    then you mean the compiler sees
    Dog other == Dog(Dog(4,4) ), as one and the same, so it immediately skips the copying and just creates an object other.

    and to clarify.. despite the elision
    both a.coolFunction(b) and a.coolFunction(Dog(1,2)) are both pass by value, yeah?.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    What function are we talking about here? to clarify do you mean the the coolFunction(....) function?
    Yes.

    Quote Originally Posted by Eman
    if that is the case
    then you mean the compiler sees
    Dog other == Dog(Dog(4,4) ), as one and the same, so it immediately skips the copying and just creates an object other.
    I am hesitant to say yes to this because you need to get the cause and effect right. The compiler does not "(skip) the copying and just creates an object other" because it "sees Dog other == Dog(Dog(4,4) ), as one and the same". Rather, it sees an opportunity to optimise, so it "skips the copying and just creates an object other", thus making it such that "Dog other == Dog(Dog(4,4) ) (are) one and the same".

    Quote Originally Posted by Eman
    both a.coolFunction(b) and a.coolFunction(Dog(1,2)) are both pass by value, yeah?.
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by laserlight View Post
    I am hesitant to say yes to this because you need to get the cause and effect right. The compiler does not "(skip) the copying and just creates an object other" because it "sees Dog other == Dog(Dog(4,4) ), as one and the same". Rather, it sees an opportunity to optimise, so it "skips the copying and just creates an object other", thus making it such that "Dog other == Dog(Dog(4,4) ) (are) one and the same".

    Best explanation ever. Now i understand what you mean.
    A heartfelt Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Temporary object debacle
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:37 PM
  3. calling constructor crashes program?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 07-04-2003, 11:17 AM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. Calling an object in a function
    By fry in forum Game Programming
    Replies: 6
    Last Post: 08-10-2002, 11:31 PM