#include<iostream>
using namespace std;
class time1
{
public:
time1(int a=0, int b=0, int c=0) :hour(a),min(b),sex(c) {}
void display();
void show()const;
void set(int a, int b, int c) {
hour = a;
min = b;
sex = c;
}
private:
int hour, min, sex;
};
void time1::display()
{
cout << hour << ":" << min << ":" << sex << endl;
}
void time1::show() const
{
cout << hour << ":" << min << ":" << sex<<endl;
}
int main() {
time1 t1(10, 20, 55),t2,*p2;
time1* const p1 = &t1;//指向对象的常指针
p2 = &t2;
//p1=&t2; 报错,指向的地址不可改
*p1 = *p2;
t1 = t2;
p1->display();
//------------
time1 t3(21,22,45), t4,*p4;
p4 = new time1{10,30,55};
const time1* p3 = &t3;//指向常对象的指针变量
// *p3 = *p4; 报错 不可改变
p3 = &t4;
p3 = p4;
p3->show();
return 0;
}
结果:
0:0:0
10:30:55
补充:如果一个变量已被声明为常变量,只能用指向常变量的指针变量来指向他。
呀呀呀再来补充一个表:它们做形参和实参时候是否合法