对explicit关键字的认识:
#include<iostream> using namespace std; class A { friend ostream& operator<< (ostream& out, const A& rhs); public: explicit A(int _v); bool isSame(A a); private: int v; }; A::A(int _v):v(_v) {} ostream& operator<< (ostream& out, const A& rhs) { return out<<rhs.v; } bool A::isSame(A rhs) { return this->v == rhs.v; } int main() { A a1(10); A a2(20); cout<<"a1:"<<a1<<endl; cout<<"a2:"<<a2<<endl; cout<<a2.isSame(a1)<<endl; // cout<<a1.isSame(10)<<endl; return 0; }
对muable;关键字的认识:
#include<iostream> class A { public: A(); void f() const; private: int i; mutable int j; }; A::A():i(0), j(0) {} void A::f() const{ // i++;//error const member function; j++;//ok:mutable } int main() { A a; a.f(); return 0; }