KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q155046: FIX: Member Function Referenced Without () Compiles

Article: Q155046
Product(s): Microsoft C Compiler
Version(s): 4.0,4.1,4.2,5.0,6.0
Operating System(s): 
Keyword(s): kbCodeGen kbCompiler kbCPPonly kbVC kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600
Last Modified: 11-FEB-2002

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual C++, versions 4.0, 4.1 
- Microsoft Visual C++, 32-bit Enterprise Edition, versions 4.2, 5.0, 6.0 
- Microsoft Visual C++, 32-bit Professional Edition, versions 4.2, 5.0, 6.0 
- Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
-------------------------------------------------------------------------------

SYMPTOMS
========

Leaving the parentheses off a member function call, or leaving the ampersand
("&") off a reference to a pointer to a member function, does not result in
a compiler error.

CAUSE
=====

In both cases, the Visual C++ compiler is incorrectly treating the reference as
a pointer to the member function. A pointer to a member function cannot be
referenced via a specific instance of a class (for example, "obj.MyFunct" in the
sample code below). Instead, it must be referenced via the class name, scope
operator and '&' operator (for example, &CMyClass::MyFunc in the sample
code below).

RESOLUTION
==========

The compiler should report an error on the line in question.

STATUS
======

Microsoft has confirmed this to be a bug in the Microsoft products listed at the
beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.

MORE INFORMATION
================

Sample Code
-----------

  /* Compile options needed:
  */ 

  class CMyClass
  {

    public:

    void MyFunc() {int nVar = 100; }
  };

  void main()
  {
    CMyClass obj;

    obj.MyFunc();  //Correct syntax - calls the member function
    obj.MyFunc;    //Typo, no (), should result in error but doesn't
                   //VC 5.0: correct warning C4551
    CMyClass::MyFunc;  // Typo, no &, should result in error but doesn't
    &CMyClass::MyFunc; // Correctly treated as a pointer to member
                       // function.
  }

Note that executing the program generated by the above code causes no run- time
errors, but executes MyFunc only once. The incorrect line has no affect because
it is treated exactly the same as the line following it.

Also note that this problem could seem to cause incorrect execution paths if the
parenthesis are left off accidentally in the following type of construct:

     if (obj.MyFunc) {   // Should be an error, instead always evaluates to
                         // TRUE. This code always executes.
     }
     else {
         // This code will never be reached.
     }

Additional query words: kbVC420bug kbVC400bug

======================================================================
Keywords          : kbCodeGen kbCompiler kbCPPonly kbVC kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600bug kbNoUpdate 
Technology        : kbVCsearch kbVC400 kbAudDeveloper kbVC410 kbVC420 kbVC500 kbVC600 kbVC32bitSearch kbVC500Search
Version           : :4.0,4.1,4.2,5.0,6.0
Issue type        : kbbug
Solution Type     : kbfix

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.