Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!ptsfa!ihnp4!alberta!calgary!lomow From: lomow@calgary.UUCP (Greg Lomow) Newsgroups: comp.lang.c++ Subject: :: operator Message-ID: <900@vaxb.calgary.UUCP> Date: Sun, 3-May-87 14:00:54 EDT Article-I.D.: vaxb.900 Posted: Sun May 3 14:00:54 1987 Date-Received: Sun, 10-May-87 09:05:33 EDT Distribution: na Organization: U. of Calgary, Calgary, Ab. Lines: 121 //////////////////////////////////////////////////////////// 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