* ambiguous type conversion
@ 2007-06-22 16:24 Shriramana Sharma
0 siblings, 0 replies; only message in thread
From: Shriramana Sharma @ 2007-06-22 16:24 UTC (permalink / raw)
To: Linux C Programming List
[-- Attachment #1: Type: text/plain, Size: 1362 bytes --]
Hello.
While reading the operator overloading chapter of Thinking in C++ I came
across the author saying that if there exist two separate definitions
for type conversion between two user-defined types, one being an
operator and the other a constructor, the compiler gives an error. But
when I tried this, I found that the compiler (at least GCC) does not
raise an error and prefers the constructor to the operator.
Please peruse the attached code. You need to compile with -DALLOWCONS to
enable the constructor:
g++ -o ambiguous ambiguous-type-conversion.cpp -DALLOWCONS
./ambiguous-type-conversion
g++ -o ambiguous ambiguous-type-conversion.cpp
./ambiguous-type-conversion
You observe that the constructor is used when present, in preference to
the operator. Is there a reason for this? I mean I would expect at least
a warning in such a case, like I get when I initialize a variable with
the extern keyword.
Shriramana Sharma.
P.S: I had to resort to somewhat ugly hacks to actually get the two
user-defined classes to define the constructor and operator -- the
example provided in the book is incomplete. One is the forward
declaration of the second class before the first class. Two is the
friend declaration of the second class within the first class. Three is
the definition of the operator of the first class *after* the second class.
[-- Attachment #2: ambiguous-type-conversion.cpp --]
[-- Type: text/x-c++src, Size: 829 bytes --]
# include <iostream>
class Superinteger ;
class Integer
{
public :
Integer ( int i ) : i__ ( i ) {}
operator Superinteger () const ; // converts Integer to Superinteger
friend class Superinteger ;
private :
int i__ ;
} ;
class Superinteger
{
public :
Superinteger ( int i ) : i__ ( i ) {}
# ifdef ALLOWCONS
Superinteger ( Integer in ) : i__ ( in . i__ ) // converts Integer to Superinteger
{ std :: cout << "Superinteger's constructor was called.\n" ; }
# endif
private :
int i__ ;
} ;
Integer :: operator Superinteger () const
{
std :: cout << "Integer's operator was called.\n" ;
return Superinteger ( i__ ) ;
}
void autoConvert ( Superinteger si )
{
std :: cout << "autoConvert ( Superinteger ) was successfully called with an Integer argument.\n" ;
}
int main ( void )
{
autoConvert ( Integer ( 20 ) ) ;
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2007-06-22 16:24 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-22 16:24 ambiguous type conversion Shriramana Sharma
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).