Define copy constructor? What is the use of copy constructor?

Discussion in 'C++' started by sunina, Feb 15, 2007.

  1. sunina

    sunina New Member

    Joined:
    Jan 31, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    # Define copy constructor? What is the use of copy constructor?
     
    Last edited by a moderator: Feb 15, 2007
  2. anantwakode

    anantwakode New Member

    Joined:
    Apr 4, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer..
    Location:
    Pune (India)
    Hi,

    look in to this for more information on copy Constructor !

    See,
    if you have a class , having pointer variables pointing to another class
    in that case if you want to new create object using existing one, and you want to use properties of existing object in to new except pointers (Otherwise both the object will point to same memory location) in that case you can use copy constructor for creating new instance from old one !

    eg.
    Code:
    class SomeClass
    {
    AnotherClass *Ac;
    public
    SomeClass(Const SomeClass &SC )
    {
    //Other Properties = SC.OthereProperties
    Ac=new AnotherClass();
    }
    };
     
    Last edited by a moderator: Feb 23, 2007
  3. anantwakode

    anantwakode New Member

    Joined:
    Apr 4, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer..
    Location:
    Pune (India)
    Hi,

    look in to this for more information on copy Constructor !
    http://en.wikipedia.org/wiki/Copy_constructor

    See,
    if you have a class , having pointer variables pointing to another class
    in that case if you want to new create object using existing one, and you want to use properties of existing object in to new except pointers (Otherwise both the object will point to same memory location) in that case you can use copy constructor for creating new instance from old one !

    eg.
    Code:
    class SomeClass
    {
    AnotherClass *Ac;
    public
    SomeClass(Const SomeClass &SC )
    {
    //Other Properties = SC.OthereProperties
    Ac=new AnotherClass();
    }
    };
    
    -Anant
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice