BUG: Function that Inherits Through Dominance FailsLast reviewed: July 31, 1997Article ID: Q116239 |
The information in this article applies to:
SYMPTOMSCode that makes use of the C++ implicit invocation of a user-defined conversion causes the C/C++ compiler to generate the following error message:
error C2594: '=' : ambiguous conversions from 'class ::E ' to 'int 'C/C++ 9.00 gives the following level 2 warning and errors from the implicit cast and only the warning from the explicit call:
warning C4250: 'E' : inherits 'B::operator`int'' via dominance error C2679: binary '=' : no operator defined which takes a right- hand operand of type 'class E' (or there is no acceptable conversion) error C2594: '=' : ambiguous conversions from 'class E' to 'int' RESOLUTIONExplicitly invoke the user-defined conversion, as demonstrated in the sample code in the "MORE INFORMATION" section below, rather then depending on the implicit invocation.
STATUSMicrosoft has confirmed this to be a bug in the products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.
MORE INFORMATIONSuppose a virtual-base class A is defined and classes B and C inherit from class A. Furthermore, a class E inherits from both B and C. The two classes, A and B, each provide a virtual user-defined conversion function to "int". The relationship may be represented by the following directed acyclic graph:
A { int () } ^ ^ / \ / \ B { int () } C {} ^ ^ \ / \ / E { }The dominance rule requires that when you invoke the virtual user-defined conversion function to int from an instance of an object of type E, the version provided by B should be used. This conversion function may be invoked implicitly by assigning an instance of an object of type E to an int. It may also be invoked explicitly through the use of the "operator int" function. As demonstrated by the following sample code, the compiler accurately identifies and invokes the correct user-defined conversion function when using the explicit call syntax. However, when attempting to make use of the implicit conversion, the compiler produces an error, citing an ambiguity in its search for the correct user-defined conversion function.
Sample Code
/* Compile options needed: /W3 */ #include <iostream.h> class A { public: virtual operator int () { return 2; } }; class B : virtual public A { public: virtual operator int () { return 3; } }; class C : virtual public A {}; class E : public B, public C {}; void main(void) { int i; int error=0; E eObject; // Both of the invocations below should call B's // operator int (). if ((i = eObject) != 3) // Fails to compile error = 1; if ((i = eObject.operator int()) != 3) // Compiles OK error = 1; if (!error) cout << "PASSED" << endl; else cout << "FAILED" << endl; } REFERENCESThe Annotated C++ Reference Manual (ARM), Ellis and Stroustrup, section 10.1.1, "Ambiguities," section 10.2, "Virtual Functions," and section 12.3, "Conversions."
|
Additional query words: 8.00 8.00c 9.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |