Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Define copy constructor? What is the use of copy constructor? (http://www.go4expert.com/forums/define-copy-constructor-copy-constructor-t3022/)

sunina 15Feb2007 12:12

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

anantwakode 23Feb2007 12:00

Re: Define copy constructor? What is the use of copy constructor?
 
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();
}
};


anantwakode 23Feb2007 12:02

Re: Define copy constructor? What is the use of copy constructor?
 
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


All times are GMT +5.5. The time now is 10:24.