Click to See Complete Forum and Search --> : A summary for constructor, copy constructor and assign operator by myself ,right or ?


flysnow
December 3rd, 2002, 02:01 AM
When ,which funcition will be called ? how about priority?


eg:

class T
{
public :
T( int );
T();
};

T fun1()
{
T t1;
return t1;
}

void fun2(T t)
{
return ;
}

T t; //constructor
T t2 = t1 ; // copy constructor

T t2 = 1 ; // constructor

t2 = t1 ; // operator =

t2 = fun() ;// copy constructor and operator =

fun2( t1) ; //copy constructor


If you define operator = funciton to accept integer parameter,

t2 = 1 ; // call operator =

otherwise
t2 = 1 ; //call constructor first and the call default operator =.






Any thing need to add ? pls give your comments ,Thanks!

Elrond
December 3rd, 2002, 09:32 AM
You have not defined a copy constructor or a copy operator. You probably should if you want this to work. And as soon as you define a copy constructor, you should define a copy operator. It is considered as good coding practice.


class T
{
T(); //Default constructor
T(int); //Other constructor
T(const T& origin); //Copy contructor
T& operator=(const T& origin); //Copy operator
}

flysnow
December 3rd, 2002, 10:08 PM
Anything else ?

galathaea
December 3rd, 2002, 10:14 PM
Seems you've got everything in order with your comments. No precedence considerations that I can see. The other post finishes things out.