CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2006
    Posts
    9

    Thumbs up Copy Constructor ????

    Hi...
    wat is Copy Constructor ??? when it is invoked ???



    Regards,
    Sundar.G

  2. #2
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: Copy Constructor ????

    Basically it is to create a deep copy of an object.

    A copy constructor takes the class name.

    Its called when you create an object from another. Or when you assign an object to another.

    Code:
    // Assume these are implemented elsewhere, for simplicity sake
    class Foo
    {
    public:
        Foo(); // Regular
        Foo(Foo &f); // Copy
    
        ...
    };
    
    Foo f1, f2;
    
    Foo f3(f1); // Copy Constructor
    
    f1.DoSomething();
    f2 = f1; // Copy Constructor
    Most compilers will provide one if you do not create it yourself, a very basic one though.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Copy Constructor ????

    Actually the assignment operator is called when making assignments to existing objects. It is a common cause of confusion.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: Copy Constructor ????

    Quote Originally Posted by TheCPUWizard
    Actually the assignment operator is called when making assignments to existing objects. It is a common cause of confusion.
    To confuse even more (and hopefully clarify by doing so):
    Code:
    A a0;
    A a1(a0);   // copy constructor
    A a2 = a1;  // copy constructor
    a2 = a1;    // assignment operator
    Jeff

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Copy Constructor ????

    Hence the reason for me using the word "existing" in my post
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: Copy Constructor ????

    Quote Originally Posted by TheCPUWizard
    Hence the reason for me using the word "existing" in my post
    And also the reason I used the word "clarify" in my post

    Jeff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured