Re: Compiler chooses conv ctor - why?



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.
=========================================================


.



Relevant Pages

  • Re: Smart pointers of const objets
    ... >>in the class constructor do not produce any output when calling foo(A ... > object directly to a reference if it is the exact type of a sub type ... (contrary to the link you provided, where a conversion is needed). ... I would assume that no compiler ...
    (comp.lang.cpp)
  • Re: a language question about conversion operators
    ... the compiler looks at the argument. ... Is its base classes one of instantiations of 'A'? ... Conversion operator is not the only means by which C can be converted to ... That class could also have a constructor from C: ...
    (microsoft.public.vc.language)
  • Re: writeObject signature
    ... > or the compiler must do a cast to Object. ... Java refers to a syntax for type conversion -- namely, ... it would be wrong (or bad design) to ...
    (comp.lang.java.programmer)
  • Re: Sets and portability (was) Re: Is ISO Pascal compatible with J&W (original) Pascal ?
    ... Or, with a good general purpose implementation, have large areas of ... > Note that ansi->wide conversion is codepage sensitive. ... form of Unicode). ... Yes, or in the absence of a CP tag, the compiler obtains the CP from the O/S ...
    (comp.lang.pascal.misc)
  • RE: pop-forum getting rid of ALL the special ved characters indocumentation files
    ... pop-11 for teaching. ... So although the Emacs conversion package will help Emacs users (who ... the compiler, the ability to keep track of changes to search lists, ... standard format, I'll make this the standard format, and make the ...
    (comp.lang.pop)

Loading