C ++ implicit and explicit initialization, type conversion

Source: Internet
Author: User

1. implicit and explicit Initialization

1.1 C ++ implicit Initialization
Int ivals (1024 );
String Hello ("Hello world .")

1.2 C ++ Explicit initialization
Int ivals = 1024;
String Hello = "Hello world ."

* PS: note that the "=" syntax here is the copy constructor rather than the value assignment operation!

Because a new object must be defined with a constructor instead of a value assignment operation.

String hello;

Hello = "Hello World"; <-here hello has been defined, which is the value assignment operation.

 

 

2. implicit and explicit conversions.

2.1
C ++ implicit conversion occurs in four situations
* In a mixed-type arithmetic expression
Int ival = 3;
Double dvals = 3.1415
Ival + dval; // ival is upgraded to double: 3.0
* Assign values using a type of expression
Int * Pi = NULL; // null (0) is converted to a null pointer of the int * type.
* Pass an expression to a function call
Extern double SQRT (double );
SQRT (2); // 2 is upgraded to double type: 2.0
* Returns an expression from a function.
Double Difference (INT ival1, int ival2)
{
Return ival1-ival2; // the return value is elevated to the double type.
}

2.2
C ++ built-in types (char, Int, short, double etc.) include implicit conversions by default between images.

2.3
C ++ user-defined class objects can contain implicit conversions.

Void dosomething (A aobject );
Class {
Public:
A (INT x = 0 );
}

Dosomething (20); // OK implicit conversion

2.4
C ++ Explicit conversions include four types
Static_cast: conversion during the compilation period. The const, volitale, or _ unaligned attribute of exdivssion cannot be converted.
* The implicit conversion between all built-in objects can be static_cast.
* Use static_cast to convert a null pointer of the target type.
* Convert any type of expression to void type using static_cast.
* You can also use static_cast to perform upstream and downstream conversions between classes. However, if a base class pointer or reference is converted to a subclass, no dynamic type check is performed, so it is not safe. the opposite is safe.

Dynamic_cast: conversion during runtime, upstream and downstream conversions between classes
* Dynamic_cast has the type check function. The downstream conversion effect is the same as that of static_cast, but the downstream conversion is safer than static_cast.
* Dynamic_cast also supports cross conversion. If the two classes share the same ancestor, their pointers can use dynamic_cast.

Const_cast: conversion during compilation, a constant in the type

Reinterpret_cast: Any pointer can be converted to another type of pointer, which can be used for conversion from char * to int *, or from one_class * To unrelated_class *. Therefore, it may be insecure.

 

2.5
C ++ built-in type pointers do not include implicit conversions (except for void *) and must be explicitly converted.

int ival = 0;
char * Pc = NULL;
int * Pi = NULL;
void * Pv = NULL;
const char * PCC = "Hello World ";
const int * PCI = & ival;
const void * PCV = NULL;

Pc = PI; // error. There is no standard implicit conversion.
Pc = reinterpret_cast (PI ); // you must use reinterpret_cast to explicitly convert the in-place mode hierarchy
Pc = PV; // error, there is no standard implicit conversion.
Pc = static_cast (PV ); // static_cast explicit conversion
Pc = PCC; // error. No standard implicit conversion is available.
Pc = const_cast (PCC ); // const_cast explicit conversion
Pc = PCV; // error. No standard implicit conversion is available.
Pc = static_cast (const_cast (PCV )); // const_cast and static_cast.

Pv = pc; // OK; implicit conversion to void *
Pv = PI; // OK; implicit conversion to void *
Pv = PCC; // error. There is no standard implicit conversion.
Pv = const_cast (PCC); // OK, const_cast explicit conversion, and char * is implicitly converted to void *
Pv = PCV; // error, there is no standard implicit conversion.
Pv = const_cast PCV; // OK, const_cast explicit conversion.

PCC = pc; // OK; implicit conversion to const char *
PCC = PI; // error. There is no standard implicit conversion.
PCC = reinterpret_cast (PI ); // you must use reinterpret_cast to explicitly convert the in-place mode hierarchy.
PCC = PV; // error. There is no standard implicit conversion.
PCC = static_cast (PV ); // static_cast explicit conversion
PCC = PCI; // error, no standard implicit conversion.
PCC = reinterpret_cast PCI; // you must use reinterpret_cast to explicitly convert the in-place mode hierarchy.
PCC = PCV; // error. There is no standard implicit conversion.
PCC = static_cast (PCV); // static_cast explicit conversion.

PCV = PV; // OK; implicitly converted to const void *
PCV = pc; // OK; implicitly converted to const void *
PCV = PI; // OK; implicitly converted to const void *
PCV = PCC; // OK; implicitly converted to const void *

2.6
Explicit conversions can eliminate unnecessary improvements.

Double dval;
Int ival;
Ival + = dval;
In this assignment, the ival is first upgraded to the double type, then added to the dval, and the result is truncated to int.
Explicit conversions eliminate unnecessary ival upgrades from int to double.
Ival + = static_cast <int> (dval );

2.7
Implicit conversions can be prohibited between user-defined objects in C ++.

Void dosomething (A aobject );
Class {
Public:
Explicit A (INT x = 0 );
}

Dosomething (20); // implicit conversion of error is forbidden.

Dosomething (static_cast <A> (20); // OK explicit conversion.

The constructor declared as explicit is generally better than non-explicit.

2.8
In combination, Explicit conversions should be used instead of implicit conversions in C ++.
Try not to use explicit reinterper_cast conversion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.