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

逛奔的蝸牛

我不聰明,但我會很努力

   ::  :: 新隨筆 ::  ::  :: 管理 ::
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 intint to floatdouble 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_castreinterpret_caststatic_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: 轉(zhuǎn)換子類的指針(或引用)為父類的指針(或引用)

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

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_castpba 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: 指針的轉(zhuǎn)換: 1. 子類和父類之間指針互相轉(zhuǎn)換(不進(jìn)行安全檢查). 非指針的轉(zhuǎn)換: 2. 標(biāo)準(zhǔn)隱式轉(zhuǎn)換(如int->float, double->int). 3. 用戶定義轉(zhuǎn)換(構(gòu)造函數(shù)轉(zhuǎn)換,轉(zhuǎn)換函數(shù))

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. 任何指針之間的相互轉(zhuǎn)換,即使這些類型之間沒有任何關(guān)系. 2. 指針和整數(shù)類型的相互轉(zhuǎn)換(指針->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: 轉(zhuǎn)換對象(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 () {
  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.

// 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 { 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 *btypeid yields their dynamic type (i.e. the type of their most derived complete object: 真實(shí)的類型,即使子類對象使用的是父類的指針,但返回的子類的信息).

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.

posted on 2010-10-08 05:25 逛奔的蝸牛 閱讀(746) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲免费在线视频| 国产一区二区| 欧美在线在线| 久久成人精品一区二区三区| 中文国产成人精品久久一| 欧美日韩国产影片| 亚洲精品国产品国语在线app| 久久精品国产亚洲精品| 久久高清国产| 久久三级视频| 亚洲欧洲精品一区| 亚洲欧美日本在线| 久久亚洲影音av资源网| 欧美精品999| 国产欧美精品一区aⅴ影院| 国产欧美一区二区精品秋霞影院| 国产一区99| 一本色道久久加勒比88综合| 亚洲欧美国产不卡| 久久尤物电影视频在线观看| 亚洲大片一区二区三区| 99国产一区二区三精品乱码| 欧美一区2区视频在线观看| 美女免费视频一区| 国产精品中文字幕欧美| 亚洲国产成人在线播放| 亚洲在线成人| 欧美二区视频| 久久成人18免费网站| 欧美日韩国产在线观看| 韩国自拍一区| 亚洲欧美日韩综合aⅴ视频| 免费欧美日韩| 性久久久久久久| 国产精品成人va在线观看| 亚洲国产精品精华液网站| 欧美在线亚洲在线| 亚洲精品五月天| 麻豆av一区二区三区久久| 国产精品推荐精品| 亚洲精品自在久久| 可以免费看不卡的av网站| 亚洲视频axxx| 欧美视频成人| 亚洲午夜电影在线观看| 欧美激情一区二区三区高清视频| 欧美一级久久久久久久大片| 欧美深夜影院| 亚洲女人天堂成人av在线| 日韩午夜在线电影| 免费成人黄色| 亚洲国产精品第一区二区| 久久三级福利| 久久成人免费日本黄色| 国产精品日韩一区二区三区| 亚洲一区黄色| 亚洲午夜视频在线观看| 欧美日韩免费观看一区二区三区 | 国产精品亚洲欧美| 亚洲少妇最新在线视频| 国精产品99永久一区一区| 欧美日韩精品高清| 1769国内精品视频在线播放| 久久久综合香蕉尹人综合网| 午夜欧美电影在线观看| 国产视频一区欧美| 久久人人看视频| 久久免费高清| 亚洲国产精品精华液2区45| 欧美成人一区二区三区片免费| 久久av二区| 国产一区二区三区黄视频| 久久精品在线| 久久国产一区二区| 在线观看日韩欧美| 欧美韩国在线| 欧美精品日韩www.p站| 一区二区激情视频| 在线视频中文亚洲| 国产亚洲一区二区三区| 久久久噜噜噜久久久| 久久久之久亚州精品露出| 亚洲国产视频一区| 亚洲精品久久久久中文字幕欢迎你| 欧美精选在线| 欧美在线亚洲在线| 欧美护士18xxxxhd| 亚洲综合色激情五月| 欧美在线视屏| 一本大道av伊人久久综合| 亚洲一区视频在线| 欧美国产大片| 国产精品手机在线| 蜜臀av一级做a爰片久久| 欧美91视频| 久久av老司机精品网站导航| 久久精品男女| 一区二区三区日韩精品| 久久er精品视频| 一本色道久久综合狠狠躁篇怎么玩 | 亚洲综合精品四区| 亚洲第一视频网站| 亚洲在线日韩| 日韩亚洲欧美高清| 久久www成人_看片免费不卡| 99re6这里只有精品| 欧美在线播放视频| 亚洲欧美99| 欧美经典一区二区三区| 久久综合给合久久狠狠色| 欧美日韩中文在线观看| 欧美大片第1页| 国产精品伦一区| 亚洲精品国产精品国产自| 狠狠综合久久av一区二区小说 | 美腿丝袜亚洲色图| 一区二区三区国产在线观看| 国产精品一区免费观看| 亚洲日本一区二区| 亚洲成色777777女色窝| 亚洲视频自拍偷拍| 怡红院精品视频| 亚洲欧美综合v| 欧美日韩在线不卡| 亚洲精品社区| 日韩小视频在线观看专区| 久久精品国内一区二区三区| 午夜视频在线观看一区| 欧美午夜视频| 亚洲伦理在线| 日韩午夜免费| 欧美激情综合色综合啪啪| 欧美高清hd18日本| 亚洲电影在线看| 欧美在线观看一区二区| 亚洲欧美日韩中文视频| 欧美色欧美亚洲另类二区| 99国产精品| 中文精品一区二区三区| 欧美日韩精品在线观看| 亚洲日本aⅴ片在线观看香蕉| 亚洲国产婷婷香蕉久久久久久| 久久久成人精品| 欧美大成色www永久网站婷| 亚洲国产精品久久久久| 免费亚洲一区| 欧美国产日产韩国视频| 亚洲伦理精品| 欧美日韩在线一二三| 99热精品在线| 久久国产免费看| 激情久久综合| 欧美大片在线观看一区| 亚洲老板91色精品久久| 亚洲欧美视频一区二区三区| 国产一区二区三区在线观看免费视频 | 黄色成人在线观看| 久久精品国产免费观看| 免费一区视频| 99国产精品视频免费观看一公开| 欧美精品综合| 亚洲欧美国产一区二区三区| 久久视频国产精品免费视频在线| 一区在线观看| 欧美激情综合在线| 在线亚洲观看| 麻豆精品在线视频| 一区二区三区产品免费精品久久75 | 欧美伦理视频网站| 日韩网站在线| 欧美在线影院在线视频| 在线观看欧美一区| 亚洲第一区在线| 亚洲在线观看视频| 一区在线播放视频| 欧美日韩妖精视频| 亚洲综合视频1区| 久久天天躁狠狠躁夜夜爽蜜月 | 欧美h视频在线| 99亚洲视频| 欧美高清视频| 久久精品国产精品亚洲精品| 亚洲第一网站| 国产精品视频yy9099| 久久影音先锋| 西西人体一区二区| 亚洲人成人一区二区在线观看 | 日韩一级精品| 国产亚洲成年网址在线观看| 欧美乱在线观看| 免费久久精品视频| 香蕉久久夜色精品国产使用方法| 亚洲国产欧美日韩精品| 久久国产日韩欧美| 宅男噜噜噜66一区二区66| 亚洲国产精品久久久| 国产综合视频| 国产亚洲毛片| 国产日韩欧美在线一区| 欧美午夜三级|