I wanna learn Copy Constructor.
So if I rely upon teh Compiler to make one I cant learn.
Please tell me wheather My Copy Constructor is Correct or not ??
Code:Point(const Point&);Code:Point::Point(const Point& pt){ *this = pt; }
I wanna learn Copy Constructor.
So if I rely upon teh Compiler to make one I cant learn.
Please tell me wheather My Copy Constructor is Correct or not ??
Code:Point(const Point&);Code:Point::Point(const Point& pt){ *this = pt; }
In a sense, it's correct. In another, it's not.
It works in this concrete case, but:
1) It doesn't work if the class contains non-assignable data, such as const members or references.
2) It doesn't work if the copy assignment operator requires a properly initialized object.
3) You can't implement copy assignment in a similar way.
4) It doesn't work if any member doesn't have a default constructor.
5) Even if they have, the results may be undesirable or simply inefficient.
As I told you in the other thread, implement an extensible array. It's simple, and it shows you what a copy constructor is actually for. You don't really learn anything from implementing trivial copy constructors.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
So would I do like this ????Code:Point::Point(const Point& pt) : x(0), y(0){}
No, that just assigns 0 to both components. You want the values of the argument. I showed you the correct implementation in another thread.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law