但是const修飾的成員變量可以在定義時初始化啊,不必通過初始化列表。
那么初始化列表的意義何在呢?
——對不同的構造函數初始化不同的const常量值嗎?
那么初始化列表的意義何在呢?
——對不同的構造函數初始化不同的const常量值嗎?
2016-09-21
視頻代碼勘誤(兩種訪問對象成員的方式):
Coordinate *p = new Coordinate[5];
for(int i = 0; i < 5; i++) {
(p + i)->x = i;
(p + i)->y = i;
p[i].printX();
p[i].printY();
}
delete []p;
p = NULL;
Coordinate *p = new Coordinate[5];
for(int i = 0; i < 5; i++) {
(p + i)->x = i;
(p + i)->y = i;
p[i].printX();
p[i].printY();
}
delete []p;
p = NULL;
2016-09-20
正確打開方式;-)
Coordinate coor;
coor.x = 10;
coor.y = 10;
coor.printX();
coor.printY();
Coordinate *p = new Coordinate();
//申請失敗
if(NULL == p){
return 0;
}
p->x = 20;
p->y = 30;
p->printX();
p->printY();
(*p).x = 30;
(*p).y = 40;
(*p).printX();
(*p).printY();
//釋放內存
delete p;
p = NULL;
Coordinate coor;
coor.x = 10;
coor.y = 10;
coor.printX();
coor.printY();
Coordinate *p = new Coordinate();
//申請失敗
if(NULL == p){
return 0;
}
p->x = 20;
p->y = 30;
p->printX();
p->printY();
(*p).x = 30;
(*p).y = 40;
(*p).printX();
(*p).printY();
//釋放內存
delete p;
p = NULL;
2016-09-17