Re: Compiler chooses conv ctor - why?
- From: changliw@xxxxxxxxxxxxxxxxxxxx (Charles Wang[MSFT])
- Date: Mon, 14 Apr 2008 08:27:42 GMT
Hi Alex,
Could you please let me know why you think that this is a compiler bug? I
think that this is just a way of compiler implementing conversion. In VC++,
by default "B b=a" will call B's constructor "B(const A& a);" to perform
the conversion; you can use "explicit" keyword before "B(const A& a);" to
avoid such automatic constructor conversion, and then the conversion would
happen on "A::operator const B() const".
Hi Cezary,
Regarding your original question, why compiler choosing conv ctor, I think
that it is related to some implicit conversion rule for compiler. When
multiple conversion methods are detected, one must be chose as with the
first priority. Unfortunately the detail of this is undocumented. Based on
my test, I think that VC++ compiler by default uses conversion constructor
prior to operator conversion. If you comment the constructor "B(const A&
a);", you will find that all conversions use your operator conversion
"A::operator const B() const" instead.
We can find some clues from MSDB documents:
Drawbacks of Conversion Constructors
http://msdn2.microsoft.com/en-us/library/1zd41sht.aspx
As the article mentioned, if it is essential to retain full control, do not
declare any constructors that take a single argument; instead, define
"helper" functions to perform conversions. So I would recommend that you
refer to this article as the best practice when you program in VC++.
If you have any other questions or concerns, please feel free to let me
know.
Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#
notif
ications.
Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
=========================================================
.
- Follow-Ups:
- Re: Compiler chooses conv ctor - why?
- From: Cezary H. Noweta
- Re: Compiler chooses conv ctor - why?
- References:
- Compiler chooses conv ctor - why?
- From: Cezary H. Noweta
- Re: Compiler chooses conv ctor - why?
- From: Alex Blekhman
- Compiler chooses conv ctor - why?
- Prev by Date: Re: Compiler chooses conv ctor - why?
- Next by Date: Re: Compiler chooses conv ctor - why?
- Previous by thread: Re: Compiler chooses conv ctor - why?
- Next by thread: Re: Compiler chooses conv ctor - why?
- Index(es):
Relevant Pages
|
Loading