我的问题是我不知道如何检查对象是否为const.我只能使用C++ 98.如何检查对象是否具有const修饰符?如何正确过载功能?
int main(){
Vec x;
const Vec y;
cout<<"Is x const? ";
y.IfConst(x); // cout << "no"
cout<<"\n";
cout<<"Is x const? ";
x.IfConst(x) // cout << "no"
cout<<"\n";
cout<<"Is y const? ";
x.IfConst(y); // cout << "yes"
cout<<"\n";
cout<<"Is y const? ";
y.IfConst(y); // cout << "yes"
cout<<"\n";
/**/
}
Run Code Online (Sandbox Code Playgroud)
我需要输出看起来像:是x const吗?不是x const?不是y const?是的是常量吗?是
我用了:
void Vec::IsConst(Vec const &vecc) const{
std::cout << "YES" << std::endl;
}
void Vec::IsConst(Vec const &vecc) {
std::cout << "NO" << std::endl;
}
Run Code Online (Sandbox Code Playgroud) c++ ×1