This pointer

This is a discussion on This pointer within the C++ Programming forums, part of the General Programming Boards category; <code> class CPoint3d {public: int x; int y; CPoint3d(const CPoint3d &pt); CPoint3d(int cx, int cy); } CPoint3d::CPoint3d(const CPoint3d& pt) { ...

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

    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,136
    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,136
    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, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21