linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shriramana Sharma <samjnaa@gmail.com>
To: Linux C Programming List <linux-c-programming@vger.kernel.org>
Subject: ambiguous type conversion
Date: Fri, 22 Jun 2007 21:54:56 +0530	[thread overview]
Message-ID: <467BF7D8.1020600@gmail.com> (raw)

[-- 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 ) ) ;
}

                 reply	other threads:[~2007-06-22 16:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=467BF7D8.1020600@gmail.com \
    --to=samjnaa@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).