青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 297,  comments - 15,  trackbacks - 0
Converting an expression of a given type into another type is known as type-casting. We have already seen some ways to type cast:

Implicit conversion

Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:

1
2
3
short a=2000;
            int b;
            b=a;


Here, the value of a has been promoted from short to int and we have not had to specify any type-casting operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow conversions such as the conversions between numerical types (short to int, int to float, double to int...), to or from bool, and some pointer conversions. Some of these conversions may imply a loss of precision, which the compiler can signal with a warning. This can be avoided with an explicit conversion.

Implicit conversions also include constructor or operator conversions, which affect classes that include specific constructors or operator functions to perform conversions. For example:

1
2
3
4
5
class A {};
            class B { public: B (A a) {} };
            A a;
            B b=a;


Here, a implicit conversion happened between objects of class A and class B, because B has a constructor that takes an object of class A as parameter. Therefore implicit conversions from A to B are allowed.

Explicit conversion

C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion. We have already seen two notations for explicit type conversion: functional and c-like casting:

1
2
3
4
short a=2000;
            int b;
            b = (int) a;    // c-like cast notation
            b = int (a);    // functional notation 


The functionality of these explicit conversion operators is enough for most needs with fundamental data types. However, these operators can be applied indiscriminately on classes and pointers to classes, which can lead to code that while being syntactically correct can cause runtime errors. For example, the following code is syntactically correct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// class type-casting
            #include <iostream>
            using namespace std;
            class CDummy {
            float i,j;
            };
            class CAddition {
            int x,y;
            public:
            CAddition (int a, int b) { x=a; y=b; }
            int result() { return x+y;}
            };
            int main () {
            CDummy d;
            CAddition * padd;
            padd = (CAddition*) &d;
            cout << padd->result();
            return 0;
            }
 


The program declares a pointer to CAddition, but then it assigns to it a reference to an object of another incompatible type using explicit type-casting:

 
padd = (CAddition*) &d;


Traditional explicit type-casting allows to convert any pointer into any other pointer type, independently of the types they point to. The subsequent call to member result will produce either a run-time error or a unexpected result.

In order to control these types of conversions between classes, we have four specific casting operators: dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses.


dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)


The traditional type-casting equivalents to these expressions would be:


(new_type) expression
new_type (expression)


but each one with its own special characteristics:

dynamic_cast


dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.

Therefore, dynamic_cast is always successful when we cast a class to one of its base classes:

1
2
3
4
5
6
7
8
class CBase { };
            class CDerived: public CBase { };
            CBase b; CBase* pb;
            CDerived d; CDerived* pd;
            pb = dynamic_cast<CBase*>(&d);     // ok: derived-to-base
            pd = dynamic_cast<CDerived*>(&b);  // wrong: base-to-derived 


The second conversion in this piece of code would produce a compilation error since base-to-derived conversions are not allowed with dynamic_cast unless the base class is polymorphic.

When a class is polymorphic, dynamic_cast performs a special checking during runtime to ensure that the expression yields a valid complete object of the requested class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// dynamic_cast
            #include <iostream>
            #include <exception>
            using namespace std;
            class CBase { virtual void dummy() {} };
            class CDerived: public CBase { int a; };
            int main () {
            try {
            CBase * pba = new CDerived;
            CBase * pbb = new CBase;
            CDerived * pd;
            pd = dynamic_cast<CDerived*>(pba);
            if (pd==0) cout << "Null pointer on first type-cast" << endl;
            pd = dynamic_cast<CDerived*>(pbb);
            if (pd==0) cout << "Null pointer on second type-cast" << endl;
            } catch (exception& e) {cout << "Exception: " << e.what();}
            return 0;
            }
Null pointer on second type-cast


Compatibility note: dynamic_cast requires the Run-Time Type Information (RTTI) to keep track of dynamic types. Some compilers support this feature as an option which is disabled by default. This must be enabled for runtime type checking using dynamic_cast to work properly.


The code tries to perform two dynamic casts from pointer objects of type CBase* (pba and pbb) to a pointer object of type CDerived*, but only the first one is successful. Notice their respective initializations:

1
2
CBase * pba = new CDerived;
            CBase * pbb = new CBase;


Even though both are pointers of type CBase*, pba points to an object of type CDerived, while pbb points to an object of type CBase. Thus, when their respective type-castings are performed using dynamic_cast, pba is pointing to a full object of class CDerived, whereas pbb is pointing to an object of class CBase, which is an incomplete object of class CDerived.

When dynamic_cast cannot cast a pointer because it is not a complete object of the required class -as in the second conversion in the previous example- it returns a null pointer to indicate the failure. If dynamic_cast is used to convert to a reference type and the conversion is not possible, an exception of type bad_cast is thrown instead.

dynamic_cast can also cast null pointers even between pointers to unrelated classes, and can also cast pointers of any type to void pointers (void*).

static_cast

static_cast can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived. This ensures that at least the classes are compatible if the proper object is converted, but no safety check is performed during runtime to check if the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, the overhead of the type-safety checks of dynamic_cast is avoided.

1
2
3
4
class CBase {};
            class CDerived: public CBase {};
            CBase * a = new CBase;
            CDerived * b = static_cast<CDerived*>(a);


This would be valid, although b would point to an incomplete object of the class and could lead to runtime errors if dereferenced.

static_cast can also be used to perform any other non-pointer conversion that could also be performed implicitly, like for example standard conversion between fundamental types:

1
2
double d=3.14159265;
            int i = static_cast<int>(d); 


Or any conversion between classes with explicit constructors or operator functions as described in "implicit conversions" above.

reinterpret_cast

reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.

It can also cast pointers to or from integer types. The format in which this integer value represents a pointer is platform-specific. The only guarantee is that a pointer cast to an integer type large enough to fully contain it, is granted to be able to be cast back to a valid pointer.

The conversions that can be performed by reinterpret_cast but not by static_cast have no specific uses in C++ are low-level operations, whose interpretation results in code which is generally system-specific, and thus non-portable. For example:

1
2
3
4
class A {};
            class B {};
            A * a = new A;
            B * b = reinterpret_cast<B*>(a);


This is valid C++ code, although it does not make much sense, since now we have a pointer that points to an object of an incompatible class, and thus dereferencing it is unsafe.

const_cast

This type of casting manipulates the constness of an object, either to be set or to be removed. For example, in order to pass a const argument to a function that expects a non-constant parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// const_cast
            #include <iostream>
            using namespace std;
            void print (char * str)
            {
            cout << str << endl;
            }
            int main () {
            const char * c = "sample text";
            print ( const_cast<char *> (c) );
            return 0;
            }
sample text


typeid

typeid allows to check the type of an expression:


typeid (expression)


This operator returns a reference to a constant object of type type_info that is defined in the standard header file <typeinfo>. This returned value can be compared with another one using operators == and != or can serve to obtain a null-terminated character sequence representing the data type or class name by using its name() member.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// typeid
            #include <iostream>
            #include <typeinfo>
            using namespace std;
            int main () {
            int * a,b;
            a=0; b=0;
            if (typeid(a) != typeid(b))
            {
            cout << "a and b are of different types:\n";
            cout << "a is: " << typeid(a).name() << '\n';
            cout << "b is: " << typeid(b).name() << '\n';
            }
            return 0;
            }
a and b are of different types:
            a is: int *
            b is: int  


When typeid is applied to classes typeid uses the RTTI to keep track of the type of dynamic objects. When typeid is applied to an expression whose type is a polymorphic class, the result is the type of the most derived complete object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// typeid, polymorphic class
            #include <iostream>
            #include <typeinfo>
            #include <exception>
            using namespace std;
            class CBase { virtual void f(){} };
            class CDerived : public CBase {};
            int main () {
            try {
            CBase* a = new CBase;
            CBase* b = new CDerived;
            cout << "a is: " << typeid(a).name() << '\n';
            cout << "b is: " << typeid(b).name() << '\n';
            cout << "*a is: " << typeid(*a).name() << '\n';
            cout << "*b is: " << typeid(*b).name() << '\n';
            } catch (exception& e) { cout << "Exception: " << e.what() << endl; }
            return 0;
            }
a is: class CBase *
            b is: class CBase *
            *a is: class CBase
            *b is: class CDerived


Notice how the type that typeid considers for pointers is the pointer type itself (both a and b are of type class CBase *). However, when typeid is applied to objects (like *a and *b) typeid yields their dynamic type (i.e. the type of their most derived complete object).

If the type typeid evaluates is a pointer preceded by the dereference operator (*), and this pointer has a null value, typeid throws a bad_typeid exception.

from:
http://www.cplusplus.com/doc/tutorial/typecasting/
posted on 2010-05-02 10:52 chatler 閱讀(555) 評論(0)  編輯 收藏 引用 所屬分類: C++_BASIS
<2009年12月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用鏈接

留言簿(10)

隨筆分類(307)

隨筆檔案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            夜夜嗨av色综合久久久综合网| 亚洲日本理论电影| 午夜在线视频一区二区区别| 日韩一级大片在线| 欧美日韩www| 欧美a级在线| 亚洲福利视频一区二区| 久久中文在线| 另类激情亚洲| 亚洲精品在线三区| 99视频日韩| 国产精品久久久久免费a∨| 亚洲午夜av电影| 亚洲尤物精选| 精品成人一区二区三区四区| 久久亚洲春色中文字幕| 另类国产ts人妖高潮视频| 夜夜嗨av一区二区三区中文字幕| 亚洲作爱视频| 国产视频一区在线| 欧美91福利在线观看| 欧美精品福利| 欧美亚洲在线播放| 久久全国免费视频| 夜夜爽夜夜爽精品视频| 在线亚洲成人| 激情伊人五月天久久综合| 亚洲第一二三四五区| 欧美人与禽猛交乱配| 欧美一区二区免费视频| 狂野欧美一区| 午夜精品福利一区二区三区av| 久久aⅴ乱码一区二区三区| 最新中文字幕一区二区三区| 一区二区三区精密机械公司 | 欧美激情偷拍| 欧美日韩亚洲另类| 久久亚洲欧美| 国产精品免费视频xxxx| 欧美成人中文字幕在线| 国产精品久线观看视频| 欧美激情视频在线免费观看 欧美视频免费一 | 欧美成人精品一区二区三区| 亚洲淫性视频| 日韩系列在线| 激情久久影院| 亚洲一区图片| 99精品99| 美女图片一区二区| 久久成人亚洲| 欧美亚洲成人精品| 亚洲激情婷婷| 伊人久久久大香线蕉综合直播 | 亚洲欧美日本伦理| 免费久久99精品国产| 欧美在线免费一级片| 欧美日韩不卡| 欧美高清视频免费观看| 国产偷国产偷精品高清尤物| 日韩一区二区久久| 99国产精品99久久久久久粉嫩| 欧美一区成人| 欧美中文在线观看| 国产精品视频大全| 亚洲私人影院| 亚洲欧美国产毛片在线| 欧美日韩精品不卡| 亚洲人成网站在线播| 亚洲黄色在线| 久久免费黄色| 久久综合九色综合网站| 国产精品色婷婷| 亚洲一区二区在线视频| 亚洲综合999| 国产精品久久久久久久app| 日韩午夜av在线| 亚洲香蕉网站| 国产精品理论片| 亚洲一区二区免费视频| 亚洲欧美日韩国产综合精品二区| 欧美日韩国产成人在线91| 亚洲三级色网| 亚洲欧美久久久| 国产精品视频999| 亚洲欧美激情在线视频| 欧美一区二区三区免费大片| 国产女人精品视频| 久久爱另类一区二区小说| 久久综合狠狠综合久久激情| 在线播放日韩| 欧美激情在线有限公司| 99视频一区| 久久精品欧美| 亚洲黄页一区| 欧美视频一区二区三区…| 亚洲伊人伊色伊影伊综合网| 久久国产色av| 亚洲国产精品va在线观看黑人 | 亚洲伊人网站| 久久夜色精品国产亚洲aⅴ | 亚洲欧美成人一区二区在线电影 | 国产欧美日本一区视频| 久久成人免费日本黄色| 欧美丰满高潮xxxx喷水动漫| 亚洲美女色禁图| 国产精品蜜臀在线观看| 久久久久se| 亚洲毛片在线观看| 久久精品视频亚洲| 亚洲精品国产精品国自产在线| 欧美三级视频| 久久久免费av| 一本一本久久a久久精品综合麻豆| 先锋影音网一区二区| 亚洲高清久久网| 国产精品一区二区视频| 模特精品在线| 欧美一区=区| 一区二区三区.www| 欧美成人日韩| 欧美在线视频不卡| 正在播放亚洲一区| 亚洲第一页自拍| 国产亚洲精品美女| 欧美日韩视频免费播放| 久久久久久国产精品一区| 亚洲天堂av电影| 日韩一级黄色大片| 欧美激情在线有限公司| 久久久无码精品亚洲日韩按摩| 亚洲视频999| 亚洲精品视频在线观看免费| 韩国精品在线观看| 国产精品有限公司| 欧美日韩综合在线免费观看| 美女视频黄免费的久久| 久久久亚洲综合| 香蕉久久国产| 亚洲女人小视频在线观看| 最新日韩在线视频| 亚洲国产色一区| 欧美国产国产综合| 免费亚洲一区| 免费观看日韩av| 免费欧美日韩| 欧美大片一区二区三区| 麻豆9191精品国产| 免费久久精品视频| 免播放器亚洲| 亚洲高清视频一区| 亚洲国产成人91精品| 欧美激情一区二区三区在线视频| 男女视频一区二区| 欧美ab在线视频| 亚洲黄色成人网| 亚洲美女精品成人在线视频| 亚洲精选视频免费看| 一本到12不卡视频在线dvd | 久久狠狠一本精品综合网| 午夜在线不卡| 久久久久久一区二区| 久久亚洲视频| 亚洲区一区二区三区| 99精品久久| 亚洲欧美在线视频观看| 欧美一区二区三区精品| 久久综合久久久久88| 欧美高清在线一区| 国产精品国产三级国产| 国产欧美日韩综合一区在线观看 | av成人天堂| 亚洲综合社区| 久久蜜桃香蕉精品一区二区三区| 米奇777超碰欧美日韩亚洲| 亚洲大胆女人| 99精品视频免费全部在线| 亚洲一区二区三区四区视频| 欧美一区二区三区视频在线观看 | 久久人人精品| 亚洲激情啪啪| 亚洲欧美中日韩| 牛人盗摄一区二区三区视频| 欧美性大战久久久久久久| 国产亚洲综合在线| 亚洲精品久久久久久久久久久| 亚洲一二三区精品| 免费视频一区| 亚洲小少妇裸体bbw| 蜜臀久久久99精品久久久久久| 欧美日韩三级| 亚洲黄色免费电影| 久久精品青青大伊人av| 亚洲狼人综合| 久久久之久亚州精品露出| 欧美体内she精视频| 在线播放精品| 久久国产日本精品| 在线亚洲精品| 欧美高清视频在线播放| 1024国产精品|