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

S.l.e!ep.¢%

像打了激速一樣,以四倍的速度運轉,開心的工作
簡單、開放、平等的公司文化;尊重個性、自由與個人價值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

C++:Type Casting

Posted on 2010-10-08 14:08 S.l.e!ep.¢% 閱讀(810) 評論(0)  編輯 收藏 引用 所屬分類: C++
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:

														
																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:

														
																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:

														
																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:

														
																// 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:

														
																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:

														
																// dynamic_cast
														
														
																#include <iostream>
														
														
																#include <exception>
														
														
																using
														
														
																namespace
														 std;

class CBase { virtualvoid 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:

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: 指針的轉換: 1. 子類和父類之間指針互相轉換(不進行安全檢查). 非指針的轉換: 2. 標準隱式轉換(如int->float, double->int). 3. 用戶定義轉換(構造函數轉換,轉換函數)

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.

														
																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:

														
																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: 1. 任何指針之間的相互轉換,即使這些類型之間沒有任何關系. 2. 指針和整數類型的相互轉換(指針->int時在Mac上會報錯: loses precision).

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:

														
																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: 轉換對象(primitive類型的不可以: int, float, double...),指針,引用的const屬性,有則去掉,沒有則加上

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:

														
																// const_cast
														
														
																#include <iostream>
														
														
																using
														
														
																namespace
														 std;

void print (char * str)
{
  cout << str << endl;
}

int main () {
  constchar * 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.

														
																// 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:

														
																// typeid, polymorphic class
														
														
																#include <iostream>
														
														
																#include <typeinfo>
														
														
																#include <exception>
														
														
																using
														
														
																namespace
														 std;

class CBase { virtualvoid 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.

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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成人| 99国内精品久久| 精品成人久久| 国产综合婷婷| 亚洲国产99精品国自产| 国产在线精品自拍| 在线免费不卡视频| 最新中文字幕一区二区三区| 亚洲激情偷拍| 一区二区高清视频| 欧美怡红院视频一区二区三区| 新67194成人永久网站| 久久男人资源视频| 亚洲国产99精品国自产| 欧美激情精品久久久六区热门| 亚洲精品日日夜夜| 亚洲欧美在线看| 久久夜色精品国产| 国产精品久久久久免费a∨大胸| 国产精品草莓在线免费观看| 国内偷自视频区视频综合| 亚洲欧美日韩精品| 亚洲三级视频| 欧美三级第一页| 国产偷国产偷亚洲高清97cao | 久久综合狠狠综合久久综青草| 久久综合五月天婷婷伊人| 91久久在线播放| 香蕉成人啪国产精品视频综合网| 久久躁狠狠躁夜夜爽| 欧美日韩综合久久| 亚洲国产成人久久综合一区| 亚洲一二三四区| 久久综合久久综合九色| 国产精品日本精品| 一本到12不卡视频在线dvd| 久久久久国产精品www| 一本色道久久综合亚洲精品不| 久久国产精品久久精品国产| 久久激情久久| 日韩午夜av| 欧美成人中文字幕在线| 亚洲男人av电影| 欧美日韩中文字幕| 亚洲精品日韩综合观看成人91| 久久久视频精品| 欧美一区二区三区啪啪| 国产精品剧情在线亚洲| 亚洲免费成人| 91久久国产综合久久| 老司机精品久久| 亚洲国产成人精品久久| 久久免费视频一区| 久久国产色av| 国产一区久久| 久久综合色婷婷| 久久婷婷蜜乳一本欲蜜臀| 很黄很黄激情成人| 久久色在线播放| 久久久久久久一区二区| 国产一区高清视频| 美女视频黄 久久| 米奇777超碰欧美日韩亚洲| 狠狠色综合一区二区| 久久久欧美一区二区| 久久免费视频网站| 亚洲黄色在线观看| 亚洲人成在线观看| 国产精品xnxxcom| 午夜免费日韩视频| 小处雏高清一区二区三区| 国产一区白浆| 欧美肥婆在线| 欧美日韩免费观看一区| 亚洲在线免费观看| 欧美亚洲在线观看| **性色生活片久久毛片| 欧美福利一区二区| 欧美日韩一区精品| 欧美一区在线视频| 久久综合九色欧美综合狠狠| 日韩视频在线观看免费| 亚洲深夜福利| 激情欧美一区二区三区| 亚洲第一精品福利| 国产精品分类| 麻豆精品精华液| 欧美午夜不卡在线观看免费| 欧美一区二区三区在线视频 | 国产日本欧美一区二区三区在线| 久久久久网址| 国产精品一区二区三区四区| 亚洲欧美激情诱惑| 久久久欧美一区二区| 妖精成人www高清在线观看| 亚洲欧美日韩在线| 亚洲欧洲日韩女同| 亚洲天堂成人在线视频| 欧美涩涩视频| 亚洲高清不卡av| 在线成人www免费观看视频| 亚洲午夜极品| 亚洲深夜激情| 国产精品亚洲第一区在线暖暖韩国| 亚洲美女中文字幕| 免费成人在线观看视频| 欧美日韩国产小视频在线观看| 欧美诱惑福利视频| 久久躁日日躁aaaaxxxx| 一区二区自拍| 欧美日韩性视频在线| 亚洲欧美日韩成人高清在线一区| 久久久久久午夜| 亚洲一区二区在线看| 亚洲尤物精选| 亚洲欧美综合精品久久成人| 亚洲性夜色噜噜噜7777| 99热这里只有精品8| 国产精品免费一区二区三区在线观看| 午夜久久电影网| 日韩视频永久免费观看| 亚洲一区二区三区高清不卡| 国产日产亚洲精品| 亚洲电影下载| 国产精品亚发布| 久久国产精品久久久| 欧美区在线观看| 亚洲国产精品视频一区| 亚洲一区二区三区在线视频| 国产精品揄拍500视频| 久久性天堂网| 亚洲一区二区三区四区在线观看 | 亚洲国产成人精品女人久久久 | 免费在线日韩av| 国产精品麻豆va在线播放| 亚洲人成在线播放| 亚洲欧洲在线一区| 欧美激情1区2区| 欧美激情精品久久久久久蜜臀| 在线播放国产一区中文字幕剧情欧美| 欧美一级播放| 久久精品72免费观看| 国产一区二区三区的电影 | 久久久久久成人| 黄色在线成人| 久久影院午夜论| 亚洲片区在线| 亚洲网站视频福利| 欧美午夜精品伦理| 亚洲性色视频| 久久一区二区三区av| 亚洲电影一级黄| 欧美日韩免费精品| 亚洲欧美伊人| 欧美成人一区二区三区片免费| 亚洲国产精品久久久久秋霞蜜臀| 老司机一区二区| 日韩视频精品在线| 久久精品二区三区| 亚洲国产日韩欧美综合久久| 亚洲资源av| 欧美午夜精品| 欧美一区二区女人| 亚洲国产91| 午夜精品久久久久久| 国产午夜精品视频| 欧美大片免费久久精品三p| 在线亚洲一区| 美女被久久久| 亚洲一区成人| 黄色av日韩| 欧美性开放视频| 久久裸体艺术| 亚洲天堂成人在线观看| 欧美大片一区二区| 亚洲欧美日韩精品久久| 在线播放视频一区| 国产精品欧美经典| 欧美成人a视频| 午夜日韩av| 99国产精品久久久| 麻豆国产精品va在线观看不卡| 亚洲视频在线视频| 亚洲国产一区二区三区在线播| 国产精品视频导航| 久久久午夜电影| 午夜欧美电影在线观看| 日韩午夜电影| 欧美成人自拍视频| 久久九九全国免费精品观看| 亚洲视频第一页| 亚洲黄色有码视频| 在线观看一区欧美| 国产日韩精品在线播放| 欧美日韩亚洲不卡| 欧美激情一二区| 亚洲最新视频在线| 欧美精彩视频一区二区三区| 亚洲电影中文字幕| 老司机一区二区三区|