C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Why in the world is this ambiguous?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
John Harrison
Guest





PostPosted: Mon Jul 07, 2003 4:46 am    Post subject: Re: Why in the world is this ambiguous? Reply with quote




"Davis King" <davisking (AT) mail (DOT) com> wrote

Quote:

The following code does not compile on any compiler I have access to
(gcc, borland, visual studio). The error I get is that
it can't convert from an int to aaa. But why in the world is this
ambiguous? It's private! Is this a compiler error or is this the
correct behavior? And if so why?

Making the private constructor explicit makes it compile but its not
like a private constructor can be used in this instance in the first
place.

class aaa
{
aaa(int*) { }
public:
aaa(unsigned short a) {}
};


int main()
{
aaa testmeme = 0;
}


-Davis


Simple. Compiler works out which function to call *before* examining whether
said functions are public or private.

This is as per the standard.

john


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Roger Orr
Guest





PostPosted: Thu Jul 10, 2003 3:53 am    Post subject: Re: Why in the world is this ambiguous? Reply with quote



"Davis King" <davisking (AT) mail (DOT) com> wrote

Quote:

The following code does not compile on any compiler I have access to
(gcc, borland, visual studio). The error I get is that
it can't convert from an int to aaa. But why in the world is this
ambiguous? It's private! Is this a compiler error or is this the
correct behavior? And if so why?

As others have said, C++ tries to find the correct function first, then sees
if you have access.

Quote:
Making the private constructor explicit makes it compile but its not
like a private constructor can be used in this instance in the first
place.

Making the constructor explicit only works because your code uses
initialisation with '='.
The statement
aaa testmeme = 0;
is valid only if a temporary aaa object can be constructed from 0, and
making a ctor explicit removes it from the possible set of ctors.
[Almost all compilers will optimise away this temporary -- and are
explicitly allowed to -- but the code is only valid if the temporary could
have been constructed]

However, if you (or other users of your class!) use direct initialisation:
aaa testmeme( 0 );
then the code remains ambiguous even when you add explicit to (either)
constructor.

You can resolve the ambiguity by specifying the exact data type:
aaa testmeme( (int*)0 );
but this is a bit messy.

Why do you need both constructors? Perhaps there's another solution to the
problem.

Regards,
Roger Orr
--
MVP in C++ at www.brainbench.com



---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.