Microsoft KB Archive/116487: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - "&" to "&")
 
(One intermediate revision by the same user not shown)
Line 74: Line 74:
*/  
*/  


#include <iostream.h&gt;
#include <iostream.h>


class B;
class B;
Line 83: Line 83:
     // This is the function the compiler
     // This is the function the compiler
     // chooses to call.
     // chooses to call.
     A(B&amp;) { cout << "called A::A(B&amp;)" << endl; }
     A(B&) { cout << "called A::A(B&)" << endl; }
};
};


Line 103: Line 103:
                 // The compiler should issue an
                 // The compiler should issue an
                 // error here but instead chooses
                 // error here but instead chooses
                 // to call A::A(B&amp;).
                 // to call A::A(B&).


         cout << "failed: should not compile- see ARM 12.3.2" << endl;
         cout << "failed: should not compile- see ARM 12.3.2" << endl;

Latest revision as of 12:25, 21 July 2020

Article ID: 116487

Article Last Modified on 7/5/2005



APPLIES TO

  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 1.5 Professional Edition
  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 5.0 Standard Edition
  • Microsoft Visual C++ 6.0 Service Pack 5
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition



This article was previously published under Q116487

SYMPTOMS

Class A has a member function that converts an instance of class B to an instance of class A. Class B also has a member function that converts an instance of class B to an instance of class A. Therefore, when you assign an instance of class B to an instance of class A, the compiler could use both conversion methods, resulting in an ambiguity. However, the C/C++ compiler does not generate an error message in this situation, as demonstrated by the sample code in the "MORE INFORMATION" section, below.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

You can use the following sample code to reproduce this problem:

Sample Code

/* Compile options needed: none
*/ 

#include <iostream.h>

class B;

class A
{
public:
    // This is the function the compiler
    // chooses to call.
     A(B&) { cout << "called A::A(B&)" << endl; }
};

class B
{
public:
     operator A()  // This function does not get called.
     {
          B b;
          cout << "called operator B::A()" << endl;
          return b;
     }
};

void main()
{
        B b;
        A a = b; // A(b) or b.operator A()?
                 // The compiler should issue an
                 // error here but instead chooses
                 // to call A::A(B&).

        cout << "failed: should not compile- see ARM 12.3.2" << endl;
}

                

REFERENCES

"The Annotated C++ Reference Manual" (ARM), Ellis and Stroustrup, Section 12.3.2, "Conversion Functions."


Additional query words: kbVC400bug 8.00 8.00c 9.00

Keywords: kbbug kbcpponly kbcompiler KB116487