Thread: CConstructor Infinite Recursion!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But it won't get to that.

    If I'm the compiler and I see

    Dog fido(rex);

    and it's calling

    Dog(Dog dog)

    well I need to make a local copy of rex for the function to use, so I call the copy constructor again, I need to make a local copy of rex for the function to use, so I call the copy constructor again, I need to make a local copy of rex for the function to use, so I call the copy constructor again, I need to make a local copy of rex for the function to use, so I call the copy constructor again ... seems like a problem!

    When you want to fully understand a call to a function, you should move in top-down order; if the computer is supposed to copy the argument, that will happen first. You need to stop thinking about what happens later in order to see the recursion.

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by whiteflags View Post
    But it won't get to that.

    well I need to make a local copy of rex for the function to use, so I call the copy constructor again, I need to make a local copy of rex for the function to use, so I call the copy constructor again, I need to make a local copy of rex for the function to use, so I call the copy constructor again, I need to make a local copy of rex for the function to use, so I call the copy constructor again ... seems like a problem!
    hello wflags.
    yep, I know it causes a recursion if I pass the object by value instead of passing the reference. You are giving me the same answer tabstops did and every other forum page has.
    But they neglect to explain <b>why</b> it is happening.
    yes, when you do this
    void fido(rex)
    the compiler needs to make a local copy of rex for the fido function to use. So it calls a copy constructor to copy the members to the new rex object (the copied object)
    so this copy constructor is called, the one we've defined(the one we "know" to be wrong)
    Code:
               Dog(Dog a )
             {
                   dog = a.weight ;
    
                }
    fine it has called the copy constructor, how does the C ctor call itself again? The compiler has made a local copy once, why would it need to make another copy again.

    I feel like I am going nowhere

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