C++进阶:新人易入的那些坑2-常量和函数

xiaoxiao2021-03-01  54

//const used with functions class Dog { int age; string name; public: Dog() { age = 3; name = "dummy"; } //const parameters void setAge(const int& a) { age = a;} void setAge(int& a) { age = a; } //const return value const string& getName() { return name;} //const function void printDogName() const { cout << name << "const" << endl; } void printDogName() { cout << getName() << "non-const" << end; } }; int main() { Dog d; int i = 9; d.setAge(i); d.printDogName(); cout << i << endl; const Dog d2; d2.printDogName(); }

 

转载请注明原文地址: https://www.6miu.com/read-4150066.html

最新回复(0)