Thread: This pointer

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    This pointer

    <code>
    class CPoint3d
    {public:
    int x;
    int y;
    CPoint3d(const CPoint3d &pt);
    CPoint3d(int cx, int cy);
    }

    CPoint3d::CPoint3d(const CPoint3d& pt)
    {
    this->x = pt.x;
    this->y = pt.y;
    }

    CPoint3d(int cx, int cy)
    {x=cx;
    y=cy;
    }

    int main()
    {CPoint3d one(2,3);
    CPoint3d two(&one);
    cout<<one.x<<endl<<one.y<<endl
    <<two.x<<endl<<two.y<<endl;
    return 0:
    }
    </code>


    Is it really necessary to include the this->variable in this copy constructor?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Nope. It's implied.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    sorry about the bad spacing, I am new to the board tags and kind of used to html. Here it is spaced out.


    Code:
    class CPoint3d 
    {public: 
        int x; 
        int y;  
        CPoint3d(const CPoint3d &pt); 
        CPoint3d(int cx, int cy); 
    } 
    
    CPoint3d::CPoint3d(const CPoint3d& pt) 
    { 
       this->x = pt.x; 
       this->y = pt.y; 
    } 
    
    CPoint3d(int cx, int cy) 
    {
      x=cx; 
      y=cy; 
    } 
    
    int main() 
    {CPoint3d one(2,3); 
      CPoint3d two(&one); 
      cout<<one.x<<endl<<one.y<<endl 
             <<two.x<<endl<<two.y<<endl; 
      return 0: 
    }

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    class CPoint3d
    {public:
        int x;
        int y; 
        CPoint3d(const CPoint3d &pt);
        CPoint3d(int cx, int cy);
    } // missing ;
    
    CPoint3d::CPoint3d(const CPoint3d& pt)
    {
       this->x = pt.x;
       this->y = pt.y;
    }
    
    CPoint3d(int cx, int cy) // missing qualifying CPoint3d::
    {
      x=cx;
      y=cy;
    }
    
    int main()
    {CPoint3d one(2,3);
      CPoint3d two(&one); // should be CPoint3d(one);  CPoint3d() takes const CPoint3d&, not CPoint3d*
      cout<<one.x<<endl<<one.y<<endl
             <<two.x<<endl<<two.y<<endl;
      return 0:
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Thanx. I am not used to OOP programming and need to get the syntax down.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    this:

    Code:
    CPoint3d::CPoint3d(const CPoint3d& pt)
    : x(pt.x),
      y(pt.y)
    {}
    is better than this:

    Code:
    CPoint3d::CPoint3d(const CPoint3d& pt)
    {
       this->x = pt.x;
       this->y = pt.y;
    }
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM