Thread: CConstructor Infinite Recursion!

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    Why is that happening?
    I mean in your code it is clear why it is happening, but in the copy constructor we wrote.
    We passed by value, yes, we do not however call the instance again...
    Except in cases where the compiler can elide the copy constructor invocation, by passing by value, you invoke the copy constructor. Do you understand this?

    Therefore, if you have a copy constructor that takes its argument by value, then before you can even get started with the copy constructor, it must invoke itself to perform a copy. But this secondary invocation cannot get started without invoking itself to perform a copy, etc.

    In my example, you say that I "induced" the recursion with the call to foo in the body of the copy constructor. In your case, you "induce" the recursion with an implied call to the copy constructor in the parameter list of the copy constructor.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    We passed by value, yes, we do not however call the instance again...
    The computer has to follow pass-by-value semantics, so interstitially, the copy constructor calls itself. You called it in another section to start the recursion.

  3. #18
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by laserlight View Post
    Except in cases where the compiler can elide the copy constructor invocation, by passing by value, you invoke the copy constructor. Do you understand this?
    yeah, I get that. our defined copy constructor will be called to do a deep copy or shallow copy as the case may be.

    [QUOTE=laserlight;991507]
    Therefore, if you have a copy constructor that takes its argument by value, then before you can even get started with the copy constructor, it must invoke itself to perform a copy. But this secondary invocation cannot get started without invoking itself to perform a copy, etc.
    [\QUOTE]

    So, when I pass by value
    this happens
    Code:
                  Dog(Dog a)
                  {
                        Dog(Dog a)//is this what happens ? 
                    }
    lol at this stage I must be making you guys pis*ed off.
    wow the author of the book didn't lie when he said it is a confusing concept to new C++ programmers.
    Last edited by Eman; 12-21-2010 at 10:08 AM.

  4. #19
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    "interstitially"
    I had to look it up. Good word.

    So unbeknown to us, a recursive call, a Dog(Dog a) call happens.
    Like how the this pointer is passed as extra argument to a function?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    So, when I pass by value
    this happens
    Not quite. Allow me to digress. A rule of thumb is that when you want to pass an object of class type to a function but not modify it, you pass it by const reference, e.g.,
    Code:
    void foo(const Dog& dog)
    {
        // ...
    }
    This avoids unnecessary copying. Sometimes, you really do want to make a copy to work on, so you write:
    Code:
    void foo(const Dog& dog)
    {
        Dog temp(dog);
        // Now we can make changes to temp.
        // ...
    }
    But you could also have written:
    Code:
    void foo(Dog dog)
    {
        // Now we can make changes to dog.
        // ...
    }
    So, what happens is closer to defining the copy constructor like this:
    Code:
    Dog(const Dog& a)
    {
        Dog temp(a);
    }
    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

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, we know.

    Code:
    Dog(Dog a)
    The call happens here. You don't even get to enter the copy constructor proper because the initial call will be waiting on subsequent calls that return a copy of a to copy again, forever. That's what happens in theory (because the compiler doesn't let you do this).

    Your analogy about the this pointer is probably wrong. It's more like... you don't see the brick wall of logic that has firmly been placed.

  7. #22
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by laserlight View Post

    Code:
    Dog(const Dog& a)
    {
        Dog temp(a);
    }
    I don't see how Dog temp(a) would still allow the function call to the constructor to never end.

    i will imagine to be something like this
    Code:
    Dog temp(Dog &a)
    {
          a.x = ....//modify a 
    
           return a ; //return object
    }
    EDIT:

    oh wait I think I can see how the function call to temp would actually make it recursive.

    Dog &a would call the copy constructor again..
    so the compiler generates this temp function to be part of the constructor ?

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    I don't see how Dog temp(a) would still allow the function call to the constructor to never end.
    Try it. Place debug print statements before and after that statement, if you must. You need to convince yourself that the copy constructor is invoked there, right in the body of the copy constructor, resulting in infinite recursion.
    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

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You are making an object called temp, not a function called temp.

  10. #25
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by whiteflags View Post
    You are making an object called temp, not a function called temp.
    oh i just embarrassed myself.
    Thanks for pointing that out

  11. #26
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by laserlight View Post
    Try it. Place debug print statements before and after that statement, if you must. You need to convince yourself that the copy constructor is invoked there, right in the body of the copy constructor, resulting in infinite recursion.
    whiteflags just pointed out that i was declaring an object, i confused it with a function , embarrassingly for me

    so a temp object is created which in turn calls the copy constructor which in turn creates a temp object.

    So the compiler does create extra code, it declares a temp object?

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    So the compiler does create extra code, it declares a temp object?
    Remember, your proposed code is illegal. We have only been looking at analogies.
    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

  13. #28
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by laserlight View Post
    Remember, your proposed code is illegal. We have only been looking at analogies.
    yeah, i meant theoretically.
    Assuming using this invalid method, the compiler merely issued a warning,
    all this proposed scenario would have occurred?
    Last edited by Eman; 12-21-2010 at 11:07 AM.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    Assuming using this invalid method, the compiler merely issued a warning,
    all this proposed scenario would have occurred?
    Probably.
    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

  15. #30
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by laserlight View Post
    Probably.
    Cool, i can sleep well knowing that then

    Also, another stuff that had been bothering me.
    I noticed that most tutorials always overloaded the operator '=' when creating a copy constructor.
    The code is an exact same which makes me wonder why the replication? What is the point of using an overloaded operator at all?
    I could only think of 2 scenarios.
    1. A programmer decide to pass or return value - where the copy constructor would be needed.
    2. A programmer might do this naturally
    Dog a = b;
    hence overloading the = operator, however he/she might also do this Dog a(b) ... so I guess it is a good idea to do have both methods defined?

    and I was wondering for the second scenario.

    Dog a = b ;

    is the constructor for the 'a' object not called twice?
    The default constructor(if explicitly defined or compiler creates one) and then the copy constructor?

    Thanks for helping me out with these concepts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  5. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM