|
-
December 3rd, 2002, 03:01 AM
#1
A summary for constructor, copy constructor and assign operator by myself ,right or ?
When ,which funcition will be called ? how about priority?
Code:
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!
Last edited by flysnow; December 3rd, 2002 at 03:06 AM.
-
December 3rd, 2002, 10:32 AM
#2
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.
Code:
class T
{
T(); //Default constructor
T(int); //Other constructor
T(const T& origin); //Copy contructor
T& operator=(const T& origin); //Copy operator
}
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
-
December 3rd, 2002, 11:08 PM
#3
-
December 3rd, 2002, 11:14 PM
#4
nothing else really
Seems you've got everything in order with your comments. No precedence considerations that I can see. The other post finishes things out.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|