#include <iostream> using namespace std; class TSB { //-----key point 1 friend TSB fnbase(); //-----key point 2 protected: TSB() { cout<<"TSB()"<<endl; } //-----key point 3 TSB(const TSB&other) { cout<<"TSB(const TSB&other)"<<endl; } public: ~TSB() { cout<<"~TSB()"<<endl; } };
class TS:public TSB { public: TS() { cout<<"TS()"<<endl; } //-----key point 4 TS(const TSB&other) { cout<<"TS(const TSB&other)"<<endl; } TS(const TS&other) { cout<<"TS(const TS&other)"<<endl; } ~TS() { cout<<"~TS()"<<endl; } }; TSB fnbase() { return TSB(); } TS fn() { //in gcc 4.2.2, the following statement will cause compiling error. /* 1.cpp: In function 'TS fn()': 1.cpp:12: error: 'TSB::TSB(const TSB&)' is protected 1.cpp:52: error: within this context */ //But it works in gcc 3.4. //--- return TS(fnbase()); //It seems that in gcc4.2.2. "return TS(fnbase());" will convert to the following two statement //.. //TSB ret = fnbase(); //return TS(ret); } int main() { TS ts = fn(); return 0; } _______________________________________________ help-gplusplus mailing list help-gplusplus@gnu.org http://lists.gnu.org/mailman/listinfo/help-gplusplus