CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2001
    Posts
    25

    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.

  2. #2
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    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

  3. #3
    Join Date
    Jan 2001
    Posts
    25
    Anything else ?

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747

    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
  •  





Click Here to Expand Forum to Full Width

Featured