Thread: Copy Constructors... *Newbie Alert!*

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    Copy Constructors... *Newbie Alert!*

    When do they get called?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Write some test code and see...

    gg

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    When does this copy constructor get called? and why..

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    int linecount = 1;
    char label = 'A';
    
    class Data
    {
        char ID;
        int value;
    public:
    
        // Default constructor
        Data()
        {
           ID = label++;
           value = 0;
           cout << setw(2) << linecount++ 
              << ": Default Constructor " << ID << " " << value << endl; 
        }
    
        // Parametrized Constructor
        Data(int v)
        {
           ID = label++;
           value = v;
           cout << setw(2) << linecount++ 
              << ": Parametrized Constructor " << ID << " " << value << endl; 
        }
    
        // Copy Constructor
        Data(const Data &d)
        {
           // copy only the value, we get a unique ID
           ID = label++;
           value = d.value;
           cout << setw(2) << linecount++ 
              << ": Copy Constructor " << ID << " " << value 
              << " From " << d.ID << " " << d.value << endl; 
        }
    
      // Assignment Operator
      Data &operator=(Data &d)
      {
        // copy only the value
        value = d.value;
        cout << setw(2) << linecount++
             << ": Assignment of " << ID << " " << value
             << " From " << d.ID << " " << d.value << endl;
        return *this;
      }
    
    
        // Destructor
        ~Data()
        {
           cout << setw(2) << linecount++ 
              << ": Destructor " << ID << " " << value << endl; 
        }
    };
    
    
    // A global function which uses Data objects
    Data Mangle(Data d)
    {
         Data temp = d;
         return temp;
    }
    
    
    // The main program.
    int main()
    {
       Data a(10);
       Data b = a;
       Data c;
    
       c = Mangle(a);
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    hmmm

    Data b = a;

    it copies the members of A into the "this" object.. which is b. Thus the copy constructor is called. oic

    but why isnt the assignment operator called..

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    hahah.. n/m got it.


    thx for the help.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Initialization is translated to a copy construction even if in the syntax
    type name = existing;
    This line has the same effect as
    type name(existing);
    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
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Initialization is translated to a copy construction even if in the syntax
    type name = existing;
    This line has the same effect as
    type name(existing);
    What if the copy constructor was explicit?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Can it be?
    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

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    oh yes.... look
    Code:
    class A
    {
       public:
         explicit A(const A&) {//impl}
       private:
         //members
    };
    
    int main()
    {
       A obj;
       A copy(obj); // fine direct call of cc
       A copy2 = obj; // error!
       return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But if it is, can it still be used for arg-by-value? That's an implicit call after all.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  2. Help with copy constructors
    By nessie in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 07:50 AM
  3. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 07:11 AM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM