Copy Constructor


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Copy Constructor

  1. #1
    Join Date
    Apr 2005
    Posts
    1

    Question Copy Constructor

    Hi All,

    I don't understand how Copy Constructor will become recursive if we define the copy constructor with call by value concept. We cannot create like that because compile would not allow to create it.

    Can anybody tell me Why we should pass by reference for the copy constructor??

    For instance,

    class A
    {
    private:

    ......
    public:
    A()
    {}
    A(A &a)
    {
    } --> This is the correct behaviour. How this would be recursive if we remove the reference (&) in the copy constructor.

    Appreciate your reply.

    Thanks in advance,
    -Subu

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi Subu,

    The copy constructor is a concept to enable you to do call-by-value for a custom class. So defining a CC that takes a parameter by value would obviously call itself.
    You are right that most (probably all) compilers would complain if you tried to create a CC with Call by value parameter, so you are safe here. There is another good reason to have a call by reference parameter:- efficiency. A reference is really nothing but a "pointer to an EXISTING object" so at runtime there won't be a copy created. Saves time and memory.
    For an in depth discussion of CC see Volume 1 of "Thinking in C++" by Bruce Eckel. You can download the whole book as HTML from Bruce's web-site.

    Hope that helps?

    Cheers,

    Dieter

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    When you pass an object by value to a function, the callee gets a *copy* of the original object. This is hardly surprising because the same thing happens for example when you pass int by value. The major difference however between pasisng int and a class object is that when you pass the latter by value, the copy is created by invoking a copy-constructor. I other words, in order to an object x to a copy constructor of class X, the compiler must first invoke a copy constructor to create the argument for the copy constructor. And how would that be accomplished? You guessed: by invoking the copy-constructor for the second copy constructor, repeat ad lib.

    Because of this, C++ requires that copy constructors shall not take arguments by value. I believe that every decent compiler these days rejects such code anyway. However, now you can see that this restriction makes a lot of sense...
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center