Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Implicit/explicit conversion

Reply
Thread Tools

Implicit/explicit conversion

 
 
al
Guest
Posts: n/a
 
      01-05-2004
1. "Implicit conversion happens in three situations: expression, passing in
arguments of functions and returning expression from function." What is
"returning expression from function" here?

2. This is what my learning about conversion in C++:
Generally in C++, casting should be avoided.
Use explicit conversion rather than implicit conversion.
Use static_cast rather than dynamic_cast or reinterpret_cast, especially
when such conversion is safe during coding.
Use dynamic_cast with casting in a class hierarchy.

Is the above understanding correct conceptually?

3. Is the reason that reinterpret_cast should be avoided that it is system
dependent? What is system dependent here regarding reinterpret_cast? Is
there any other reason?

4. "The static_cast is available is available for a conversion that is
portable, well-defined, and invertible." Could someone help me understand
this statement better?

Thanks!



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-05-2004
"al" <(E-Mail Removed)> wrote...
> 1. "Implicit conversion happens in three situations: expression, passing

in
> arguments of functions and returning expression from function." What is
> "returning expression from function" here?


double foo() {
return 42;
}

The expression '42' here is an int. It will be implicitly converted
to double during function return.

>
> 2. This is what my learning about conversion in C++:
> Generally in C++, casting should be avoided.
> Use explicit conversion rather than implicit conversion.
> Use static_cast rather than dynamic_cast or reinterpret_cast, especially
> when such conversion is safe during coding.
> Use dynamic_cast with casting in a class hierarchy.
>
> Is the above understanding correct conceptually?


I cannot claim full understanding why you have "use explicit rather
than implicit" there. 'static_cast' is a compile-time conversion.
In a class hierarchy, derived is converted to base implicitly (often,
if possible, anyway), using 'dynamic_cast' for that is overkill. I
don't know if this is something you expected as an answer.

>
> 3. Is the reason that reinterpret_cast should be avoided that it is system
> dependent? What is system dependent here regarding reinterpret_cast? Is
> there any other reason?


The Standard says that reinterpret_cast is only guaranteed to work for
conversion A->B->A, and everything else may not work (implementation-
defined).

>
> 4. "The static_cast is available is available for a conversion that is
> portable, well-defined, and invertible." Could someone help me understand
> this statement better?


Where did you find it? Is appearance of "is available" twice a typo?

Usually static_cast is used for a conversion T1->T2 if T2->T1 is implicit.
That's "invertible" part. What they mean by well-defined or portable, I
don't know. FWIW, every C++ program is portable unless it uses some kind
of implementation-defined properties (and it still may be portable, just
not guaranteed to be).

Victor


 
Reply With Quote
 
 
 
 
Ernst Murnleitner
Guest
Posts: n/a
 
      01-07-2004
> > 4. "The static_cast is available is available for a conversion that is
> > portable, well-defined, and invertible." Could someone help me

understand
> > this statement better?

>
> Where did you find it? Is appearance of "is available" twice a typo?


I also wondered once. I guess it was in the Stroustroup.

Greetings
Ernst



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
Date conversion problem with OE importing saroxonline76@vodafone.it Firefox 0 07-12-2005 07:38 PM
Framework 1.0 to 1.1 conversion Luc Bisson ASP .Net 2 11-19-2003 02:40 AM



Advertisments