Copy constructors
C++ will call the copy constructor for class T when:
*An instance of class T is created from an existing
instance of class T.
For example:
*A function is called and an argument of class T is
passed by value:
*An unnamed temporary variable is created to hold the
instance of class T returned by some function.
class T {
char* p;
public:
T()
{};// the null(default) constructor
T( char* s )
{ p = strdup( s ); }
~T()
{ delete p; }
};
T foo( T c ) // definition of function foo()
{ return c; };
int main() {
T* a = new T( "hello" );
|