lomow@calgary.UUCP (Greg Lomow) (05/03/87)
//////////////////////////////////////////////////////////// The following comments apply to the 1.2 release of C++ running on Unix 4.3BSD. //////////////////////////////////////////////////////////// /* The following program defines two functions named func(), one is globally defined and the other is a member of class cl. */ int func() { return 1; } class cl { public: cl(); int func(); }; cl::cl() { printf("%d %d %d\n", ::func(), // global function func(), // member function cl::func()); // member function } int cl::func() { return 2; } main() { cl *cl1 = new cl(); printf("%d %d %d\n", ::func(), // global function func(), // global function cl1->func()); // member function } /* When I run the program I get the answer I expect 1 2 2 1 1 2 */ //////////////////////////////////////////////////////////// /* If I modify the program and remove the definition for the member function func(), then I would expect the compiler to issue an error message when it encounters the following line in cl::cl() cl::func()); // member function Instead the program compiles without error, runs, and produces the following output 1 1 1 1 1 I also tried adding the member function func_test() to see if this problem was peculiar to constructor functions; once again no error message was issued. */ int func() { return 1; } class cl { public: cl(); void func_test(); }; cl::cl() { printf("%d %d %d\n", ::func(), // global function func(), // global function cl::func()); // member function } void cl::func_test() { printf("%d %d %d\n", ::func(), // global function func(), // global function cl::func()); // member function } main() { cl *cl1 = new cl(); cl1->func_test(); printf("%d %d\n", ::func(), // global function func()); // global function } //////////////////////////////////////////////////////////// /* The reason I expect an error message is that there is no function with the identifier func() which is a member of the class cl. I see three possibilities: 1) the :: operator really starts at the qualification level cl and looks in cl and its base classes for the definition of func() and if it does not find it, :: continues to look for func() in the globally defined functions. If this is the case then the semantics of the :: operator are not fully explained in the C++ book. 2) This is a bug in the compile time checking that is done in conjunction with the :: operator. 3) I don't understand something. Please straighten me out. */ //////////////////////////////////////////////////////////// /* On a separate point, it does not seem to matter to the compiler if I code cl *cl1 = new cl; // NOTE NO '()' or cl *cl1 = new cl(); // NOTE '()' Which is correct? or are both ok? */ //////////////////////////////////////////////////////////// Greg Lomow Usenet: ....![ubc-vision,ihnp4]!alberta!calgary!lomow