-
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?
-
-
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:
}
-
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:
}
-
Thanx. I am not used to OOP programming and need to get the syntax down.
-
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.