Hi, I got a question here. When I created a class using some sort of member functions, I could hardly distinguish the constness between overloaded ones. For example, I tried to call the const function from the nonconst one, while the compiler did some recursive call and caused the stack overflowed.
#include <iostream>
#include <string>
usingnamespace std;
class Test{
string text="hello,world!";
public:
const Test &func(ostream &os)const {os<<text; return *this;}
Test &func(ostream &os){returnconst_cast<Text &> (func(os));}
//call the func() with const qualifier, but turn out to be a recursive call
}
int main(){
Test t1;
const Test t2;
t2.func(cout); //print the string
cout<<endl;
t1.func(cout); //won't yield "hello,world!"
return 0;
}