BUG: No Compiler Error for Ambiguous C++ ConversionLast reviewed: July 24, 1997Article ID: Q116487 |
The information in this article applies to:
SYMPTOMSClass 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.
STATUSMicrosoft has confirmed this to be a bug in the products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.
REFERENCES"The Annotated C++ Reference Manual" (ARM), Ellis and Stroustrup, Section 12.3.2, "Conversion Functions."
MORE INFORMATIONYou 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; } |
Additional query words: 8.00 8.00c 9.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |