ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

const函数

const函数

1.const对象只能调用const函数

//Human.h
class Human {
public:
......
void description() const; //注意,const的位置-------------------------》重要
......
};

//Human.cpp
void Human::description ()const {
cout << "age:" << age
<< " name:" << name
<< " salary:" << salary
<< " addr:" << addr
<< " bloodType:" << bloodType << endl;
}

//main.cpp
int main(void) {
const Human h1;-------------------------》重要
h1.description();

system("pause");
return 0;

}

返回列表