r*****r 发帖数: 397 | 1 One rule for constructor:use initialization instead of assignment. Is this
also true for object?For example,I have a simple class
Point{
public:
...
private:
float x;
float y;
}
Suppose I have another class and want to use a point as a private member,
PointColl{
public:
//default constructor
PointColl();
PointColl(const PointColl & thePoint);
...
private:
int number;
Point* thePointer;
}
For the default constructor, can I do like this
PointColl::PointColl():number(0)
{
thePointer = n | c*****t 发帖数: 1879 | 2 Use the one declaring the initiation parameters before { }
【在 r*****r 的大作中提到】 : One rule for constructor:use initialization instead of assignment. Is this : also true for object?For example,I have a simple class : Point{ : public: : ... : private: : float x; : float y; : } : Suppose I have another class and want to use a point as a private member,
| y****i 发帖数: 156 | 3 Yes, the rule applied to object.
Reason | r*****r 发帖数: 397 | 4 Thank you.And I understand the rule, just not sure about the syntax.for
example,in default constructor,
PointColl::PointColl():
number(0),
thePointer(NULL)
{
}
thePointer(NULL) is equal to " thePointer = new Point();"?I mean, this will
create a new pointer.
For another constructor,I can use directory
PointColl::PointColl(int num,const Point& p):
number(num)
thePointer(p);
{
}
instead of
PointColl::PointColl(int num,const Point& p):number(num)
{
thePointer = new Point(p);
}
Thanks, again | r*****r 发帖数: 397 | 5 I mean, I tried for both case in the constructor
PointColl::PointColl(int num,const Point& p):
number(num)
thePointer(p);
{
}
and get an error "cannot convert 'Point' to 'Point*' in initialization
but if i use
PointColl::PointColl(int num,const Point& p):number(num)
{
thePointer = new Point(p);
}
it works fine.
why?
.
【在 y****i 的大作中提到】 : Yes, the rule applied to object. : Reason
| y****i 发帖数: 156 | 6
I mean, I tried for both case in the constructor
PointColl::PointColl(int num,const Point& p):
number(num)
thePointer(p);
{
}
and get an error "cannot convert 'Point' to 'Point*' in initialization
//---------------------------------
thePointer is a pointer(Point*) , however p is an object(Point)
You cannot assign a object to a pointe. If you really want to, you have to
cast it like thePoint = (Point*) p; // dangerous
Are you clear?
but if i use
PointColl::PointColl(int num,const Point& p):numbe
【在 r*****r 的大作中提到】 : I mean, I tried for both case in the constructor : PointColl::PointColl(int num,const Point& p): : number(num) : thePointer(p); : { : } : and get an error "cannot convert 'Point' to 'Point*' in initialization : but if i use : PointColl::PointColl(int num,const Point& p):number(num) : {
| r*****r 发帖数: 397 | 7 Yeah, very clear. So basically, for pointers, better use new to create instead
of initialization.Right?Thank you .
【在 y****i 的大作中提到】 : : I mean, I tried for both case in the constructor : PointColl::PointColl(int num,const Point& p): : number(num) : thePointer(p); : { : } : and get an error "cannot convert 'Point' to 'Point*' in initialization : //--------------------------------- : thePointer is a pointer(Point*) , however p is an object(Point)
| y****i 发帖数: 156 | 8 Yes.
For object, it is better to init in init list
for builtin types or pointer, you can use both ways.
However, let p = new A() int constructor is much clear that in the init-list.
for pointers, better use new to create
instead: of initialization.Right?Thank you .
【在 r*****r 的大作中提到】 : Yeah, very clear. So basically, for pointers, better use new to create instead : of initialization.Right?Thank you .
|
|