// DEM_CON.CPP -- demostrate use of const #include class DC { public: void mf1(const DC &dc) { cout << "inside mf1" << endl; } void mf2(DC &dc) { cout << "inside mf1" << endl; } void mf3() const { cout << "inside mf2" << endl; } // won't compile because const dc returned by ref // DC& mf4(const DC& dc) { cout << "inside mf4" << endl; return dc; } // This compiles !!! with only a warning friend void ff1(const DC &dc) { cout << "inside ff1" << endl; } friend void ff2(DC &dc) { cout << "inside ff2" << endl; } }; void main() { DC dc; const DC cdc; // cdc.mf1(dc); // won't compile - non-const function on const object // cdc.mf2(dc); // compiles - warning non const func called on const // cdc.mf2(cdc); // compiles - warning about tempory used to initialize dc.mf3(); // won't compile - non const using const mf cdc.mf3(); // compiles ff1(dc); // won't compile - type mismatch ff1(cdc); // compiles ff2(dc); // won't compile - type mismatch // ff2(cdc); // warns only about structure passed by value!! }