Microsoft KB Archive/116487: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - "&" to "&")
 
(3 intermediate revisions by the same user not shown)
Line 54: Line 54:
== SYMPTOMS ==
== 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.
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.


</div>
</div>
Line 74: Line 74:
*/  
*/  


#include &lt;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 &lt;&lt; &quot;called A::A(B&amp;)&quot; &lt;&lt; endl; }
     A(B&) { cout << "called A::A(B&)" << endl; }
};
};


Line 92: Line 92:
     {
     {
           B b;
           B b;
           cout &lt;&lt; &quot;called operator B::A()&quot; &lt;&lt; endl;
           cout << "called operator B::A()" << endl;
           return b;
           return b;
     }
     }
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 &lt;&lt; &quot;failed: should not compile- see ARM 12.3.2&quot; &lt;&lt; endl;
         cout << "failed: should not compile- see ARM 12.3.2" << endl;
}
}


Line 115: Line 115:
== REFERENCES ==
== REFERENCES ==


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


</div>
</div>

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