Q: What are the C++ casting operators?
A: Casting means you change the representation of a variable by changing its type to a different one. In order to type-cast a simple object to another you use the traditional type casting operator. For example, to cast a floating point number of type 'double' to an integer of type 'int':
#include <typeinfo>
Code:
int i;
double d;
i = (int) d;
or also
Code:
i = int (d);
This is quite good for basic types that have standard defined conversions, however this operators can also been indiscriminately applied on classes and pointers to classes. ANSI-C++ standard has defined four new casting operators: 'reinterpret_cast', 'static_cast', 'dynamic_cast' and 'const_cast' in order to control these types of conversions between classes...
Code:
reinterpret_cast<new_type>(expression)
Code:
static_cast<new_type>(expression)
Code:
dynamic_cast<new_type>(expression)
Code:
const_cast<new_type>(expression)
'reinterpret_cast'
'reinterpret_cast' casts a pointer to any other type of pointer. It also allows casting from pointer to an integer type and vice versa.
This operator can cast pointers between non-related classed. The operation results is a simple binary copy of the value from a pointer to the other. The content pointed does not pass any kind of check nor transformation between types.
In the case that the copy is performed from a pointer to an integer, the interpretation of its content is system dependent and therefore any implementation is non portable. A pointer casted to an integer enough large to fully contain it can be casted back to a valid pointer.
Code:
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B *>(a);
'reinterpret_cast' treats all pointers exactly as traditional type-casting operators do.
'static_cast'
'static_cast' allows to perform any casting that can be implicitly performed as well as also the inverse cast (even if this is not allowed implicitly).
Applied to pointers to classes, that is to say that it allows to cast a pointer of a derived class to its base class (this is a valid conversion that can be implicitly performed) and can also perform the inverse: cast a base class to its derivated class.
In this last case the base class that is being casted is not checked to determine wether this is a complete class of the destination type or not.
Code:
class Base {};
class Derived : public Base {};
Base *a = new Base;
Derived *b = static_cast<Derived *>(a);
'static_cast', aside from manipulating pointers to classes, can also be used to perform conversions explicitly defined in classes, as well as to perform standard conversions between fundamental types:
Code:
double d = 3.14159265;
int i = static_cast<int>(d);
'dynamic_cast'
'dynamic_cast' is exclusively used with pointers and references to objects. It allows any type-casting that can be implicitly performed as well as the inverse one when used with polymorphic classes, however, unlike static_cast, dynamic_cast checks, in this last case, if the operation is valid. That is to say, it checks if the casting is going to return a valid complete object of the requested type.
Checking is performed during run-time execution. If the pointer being casted is not a pointer to a valid complete object of the requested type, the value returned is a 'NULL' pointer.
Code:
class Base { virtual dummy() {} };
class Derived : public Base {};
Base* b1 = new Derived;
Base* b2 = new Base;
Derived* d1 = dynamic_cast<Derived *>(b1); // succeeds
Derived* d2 = dynamic_cast<Derived *>(b2); // fails: returns 'NULL'
If the type-casting is performed to a reference type and this casting is not possible an exception of type 'bad_cast' is thrown:
Code:
class Base { virtual dummy() {} };
class Derived : public Base { };
Base* b1 = new Derived;
Base* b2 = new Base;
Derived d1 = dynamic_cast<Derived &*>(b1); // succeeds
Derived d2 = dynamic_cast<Derived &*>(b2); // fails: exception thrown
'const_cast'
This type of casting manipulates the const attribute of the passed object, either to be set or removed:
Code:
class C {};
const C *a = new C;
C *b = const_cast<C *>(a);
Neither of the other three new cast operators can modify the constness of an object.
Note: The 'const_cast' operator can also change the 'volatile' qualifier on a type.
typeid 得到類(lèi)對(duì)象或指針
typeid(*pb).name() 輸出對(duì)象名
typeid(pb).name() 輸出類(lèi)名
---------子類(lèi)向基類(lèi)私有或保護(hù)繼承的時(shí),可以轉(zhuǎn)化但是不能訪(fǎng)問(wèn)。 一般不進(jìn)行轉(zhuǎn)化。error 2443
------------------------------------------------------------------------------------
C++風(fēng)格的轉(zhuǎn)型運(yùn)算符小結(jié)
為了改正C中丑陋的轉(zhuǎn)型操作,C++中引入了四個(gè)新的轉(zhuǎn)型操作符,分別是:
dynamic_cast
const_cast
static_cast
reinterpret_cast
1.dynamic_cast
這個(gè)轉(zhuǎn)型操作符主要用在安全的向下轉(zhuǎn)型(safe downcasting)中,也就是從基類(lèi)指針/引用向派生類(lèi)指針/引用的轉(zhuǎn)型。例如:
Code: [View More of this Code] [View Even More of this Code] [View Less of this Code] [Select All of this Code]
class A {...};
class B:public A {...};
A* a = new B;
B* b;
b = dynamic_cast<B *> a;
當(dāng)你將dynamic_cast用在指針上時(shí),如果成功,就傳回一個(gè)轉(zhuǎn)型目標(biāo)的指針,如果失敗,則傳回null指針。所以用到dynamic_cast的時(shí)候必然會(huì)導(dǎo)致if-then-else的程序風(fēng)格,其中else就是用來(lái)檢測(cè)轉(zhuǎn)型失敗的情況([1]第39條)。例子如下:
Code: [View More of this Code] [View Even More of this Code] [View Less of this Code] [Select All of this Code]
#include <iostream>
using namespace std;
class A {
public:
virtual void do_sth(){
cout<<"aaa\n";
}
};
class B : public A {
public:
virtual void do_sth(){
cout<<"bbb\n";
}
};
class C : public A {
public:
virtual void do_sth(){
cout<<"ccc\n";
}
};
int main(){
A* a1 = new B;
A* a2 = new C;
B* b;
if(b = dynamic_cast<B *>(a1))//轉(zhuǎn)換成功
b->do_sth();
else
cout<<"error\n";
if(b = dynamic_cast<B *>(a2))//轉(zhuǎn)換失敗
b->do_sth();
else
cout<<"error\n";
}
2.const_cast
此轉(zhuǎn)型操作符用來(lái)將對(duì)象或指針的常量性(sonstness)轉(zhuǎn)型掉。例如:
const A * a1;
A * a2 = const_cast<A *> (a1);
需要注意的是,const_cast轉(zhuǎn)型并不總是成功的,當(dāng)遇到轉(zhuǎn)型的對(duì)象本身就是const的時(shí)候,那么將其常量性轉(zhuǎn)型,結(jié)果未定義。看一下下面的例子:
Code: [View More of this Code] [View Even More of this Code] [View Less of this Code] [Select All of this Code]
// constcast.cpp from [2]
#include <iostream>
using std::cout;
void change(const int * pt, int n);
int main()
{
int pop1 = 38383;
const int pop2 = 2000;
cout << "pop1, pop2: " << pop1 << ", " << pop2 << '\n';
change(&pop1, -1);
change(&pop2, -1);
cout << "pop1, pop2: " << pop1 << ", " << pop2 << '\n';
return 0;
}
void change(const int * pt, int n)
{
int * pc;
if (n < 0)
{
pc = const_cast<int *>(pt);
*pc = 100;
}
}
輸出的結(jié)果是:
pop1, pop2: 38383, 2000
pop1, pop2: 100, 2000
可見(jiàn),pop2的值并沒(méi)有改變(有些編譯器會(huì)產(chǎn)生一個(gè)pop2的臨時(shí)變量,并將其地址值賦給pc。在C++標(biāo)準(zhǔn)中,這種情況下的行為是不確定的[2])。
3.static_cast
此轉(zhuǎn)型操作符用于內(nèi)建數(shù)據(jù)類(lèi)型之間的轉(zhuǎn)型。它與C的轉(zhuǎn)型操作最接近。當(dāng)沒(méi)有其他適當(dāng)?shù)霓D(zhuǎn)型操作符可用時(shí),就使用它。它可以將整型轉(zhuǎn)換為枚舉型,雙精度型轉(zhuǎn)換為整型,浮點(diǎn)型轉(zhuǎn)換為長(zhǎng)整型等等。例如:
Code: [View More of this Code] [View Even More of this Code] [View Less of this Code] [Select All of this Code]
int n=9;
double d = static_cast < double > (n);
上面的例子中,我們將一個(gè)變量從 int 轉(zhuǎn)換到 double. 這些類(lèi)型的二進(jìn)制表達(dá)式是不同的。要將整數(shù) 9 轉(zhuǎn)換到 雙精度整數(shù) 9,static_cast需要正確地為雙精度整數(shù) d 補(bǔ)足比特位。其結(jié)果為 9.0 。
4.reinterpret_cast
此轉(zhuǎn)型操作符的結(jié)果取決于編譯器,用于修改操作數(shù)類(lèi)型,非類(lèi)型安全的轉(zhuǎn)換符。舉例如下:
Code: [View More of this Code] [View Even More of this Code] [View Less of this Code] [Select All of this Code]
int main() { // from [2]
struct dat { short a; short b;};
long value = 0xA224B118;
dat * pd = reinterpret_cast<dat *> (&value);
cout << pd->a; // display first 2 bytes of value
return 0;
}
該例中將long型轉(zhuǎn)換成struct,但此代碼是不可移植的,在IBM兼容機(jī)和Mac機(jī)上的運(yùn)行結(jié)果完全不一樣,因?yàn)樗麄兇鎯?chǔ)字節(jié)的方式不一樣。
reinterpret_cast的使用要非常的謹(jǐn)慎,例如將3中的例子改寫(xiě)如下:
Code: [View More of this Code] [View Even More of this Code] [View Less of this Code] [Select All of this Code]
int n=9;
double d = reinterpret_cast<double & > (n);
這次,與3的結(jié)果有所不同。在進(jìn)行計(jì)算以后,d 包含無(wú)用值。這是因?yàn)?reinterpret_cast 僅僅是復(fù)制 n 的比特位到 d,沒(méi)有進(jìn)行必要的分析。
以上是我在學(xué)習(xí)這些轉(zhuǎn)型操作符一個(gè)小結(jié),如有不對(duì)之處,請(qǐng)各位不嗇賜教。
大家有興趣,可以去這里做一下有關(guān)這些轉(zhuǎn)型操作符的練習(xí)。
注:以上例子在 VC6 + Intel C++ Compiler 8.0 下編譯運(yùn)行通過(guò)(別忘了打開(kāi)RTTI,加參數(shù)/GR)。
參考文獻(xiàn):
[1] Effective C++ 中文版 Scott Meyers 侯捷譯
[2] C++ Primer Plus, 4th Edition, Stephen Prata
[3] STATIC_CAST VERSUS REINTERPRET_CAST(Topica轉(zhuǎn)載)
----------------------------------
這是More Effecitve C++里的第二條對(duì)類(lèi)型轉(zhuǎn)換講的很好,也很基礎(chǔ)好懂。
Item M2:盡量使用C++風(fēng)格的類(lèi)型轉(zhuǎn)換
仔細(xì)想想地位卑賤的類(lèi)型轉(zhuǎn)換功能(cast),其在程序設(shè)計(jì)中的地位就象goto語(yǔ)句一樣令人鄙視。但是它還不是無(wú)法令人忍受,因?yàn)楫?dāng)在某些緊要的關(guān)頭,類(lèi)型轉(zhuǎn)換還是必需的,這時(shí)它是一個(gè)必需品。
不過(guò)C風(fēng)格的類(lèi)型轉(zhuǎn)換并不代表所有的類(lèi)型轉(zhuǎn)換功能。
一、來(lái)它們過(guò)于粗魯,能允許你在任何類(lèi)型之間進(jìn)行轉(zhuǎn)換。不過(guò)如果要進(jìn)行更精確的類(lèi)型轉(zhuǎn)換,這會(huì)是一個(gè)優(yōu)點(diǎn)。在這些類(lèi)型轉(zhuǎn)換中存在著巨大的不同,例如把一個(gè)指向 const對(duì)象的指針(pointer-to-const-object)轉(zhuǎn)換成指向非const對(duì)象的指針(pointer-to-non-const -object)(即一個(gè)僅僅去除const的類(lèi)型轉(zhuǎn)換),把一個(gè)指向基類(lèi)的指針轉(zhuǎn)換成指向子類(lèi)的指針(即完全改變對(duì)象類(lèi)型)。傳統(tǒng)的C風(fēng)格的類(lèi)型轉(zhuǎn)換不對(duì)上述兩種轉(zhuǎn)換進(jìn)行區(qū)分。(這一點(diǎn)也不令人驚訝,因?yàn)镃風(fēng)格的類(lèi)型轉(zhuǎn)換是為C語(yǔ)言設(shè)計(jì)的,而不是為C++語(yǔ)言設(shè)計(jì)的)。
二、來(lái)C 風(fēng)格的類(lèi)型轉(zhuǎn)換在程序語(yǔ)句中難以識(shí)別。在語(yǔ)法上,類(lèi)型轉(zhuǎn)換由圓括號(hào)和標(biāo)識(shí)符組成,而這些可以用在C++中的任何地方。這使得回答象這樣一個(gè)最基本的有關(guān)類(lèi)型轉(zhuǎn)換的問(wèn)題變得很困難:“在這個(gè)程序中是否使用了類(lèi)型轉(zhuǎn)換?”。這是因?yàn)槿斯ら喿x很可能忽略了類(lèi)型轉(zhuǎn)換的語(yǔ)句,而利用象grep的工具程序也不能從語(yǔ)句構(gòu)成上區(qū)分出它們來(lái)。
C++通過(guò)引進(jìn)四個(gè)新的類(lèi)型轉(zhuǎn)換操作符克服了C風(fēng)格類(lèi)型轉(zhuǎn)換的缺點(diǎn),這四個(gè)操作符是, static_cast, const_cast, dynamic_cast, 和reinterpret_cast。在大多數(shù)情況下,對(duì)于這些操作符你只需要知道原來(lái)你習(xí)慣于這樣寫(xiě),
(type) expression
而現(xiàn)在你總應(yīng)該這樣寫(xiě):
static_cast<type>(expression)
例如,假設(shè)你想把一個(gè)int轉(zhuǎn)換成double,以便讓包含int類(lèi)型變量的表達(dá)式產(chǎn)生出浮點(diǎn)數(shù)值的結(jié)果。如果用C風(fēng)格的類(lèi)型轉(zhuǎn)換,你能這樣寫(xiě):
int firstNumber, secondNumber;
...
double result = ((double)firstNumber)/secondNumber;
如果用上述新的類(lèi)型轉(zhuǎn)換方法,你應(yīng)該這樣寫(xiě):
double result = static_cast<double>(firstNumber)/secondNumber;
這樣的類(lèi)型轉(zhuǎn)換不論是對(duì)人工還是對(duì)程序都很容易識(shí)別。
static_cast 在功能上基本上與C風(fēng)格的類(lèi)型轉(zhuǎn)換一樣強(qiáng)大,含義也一樣。它也有功能上限制。例如,你不能用static_cast象用C風(fēng)格的類(lèi)型轉(zhuǎn)換一樣把 struct轉(zhuǎn)換成int類(lèi)型或者把double類(lèi)型轉(zhuǎn)換成指針類(lèi)型,另外,static_cast不能從表達(dá)式中去除const屬性,因?yàn)榱硪粋€(gè)新的類(lèi)型轉(zhuǎn)換操作符const_cast有這樣的功能。
其它新的C++類(lèi)型轉(zhuǎn)換操作符被用在需要更多限制的地方。const_cast用于類(lèi)型轉(zhuǎn)換掉表達(dá)式的const或volatileness屬性。通過(guò)使用const_cast,你向人們和編譯器強(qiáng)調(diào)你通過(guò)類(lèi)型轉(zhuǎn)換想做的只是改變一些東西的 constness或者volatileness屬性。這個(gè)含義被編譯器所約束。如果你試圖使用const_cast來(lái)完成修改constness 或者volatileness屬性之外的事情,你的類(lèi)型轉(zhuǎn)換將被拒絕。下面是一些例子:
class Widget { ... };
class SpecialWidget: public Widget { ... };
void update(SpecialWidget *psw);
SpecialWidget sw; // sw 是一個(gè)非const 對(duì)象。
const SpecialWidget& csw = sw; // csw 是sw的一個(gè)引用
// 它是一個(gè)const 對(duì)象
update(&csw); // 錯(cuò)誤!不能傳遞一個(gè)const SpecialWidget* 變量
// 給一個(gè)處理SpecialWidget*類(lèi)型變量的函數(shù)
update(const_cast<SpecialWidget*>(&csw));
// 正確,csw的const被顯示地轉(zhuǎn)換掉(
// csw和sw兩個(gè)變量值在update
//函數(shù)中能被更新)
update((SpecialWidget*)&csw);
// 同上,但用了一個(gè)更難識(shí)別
//的C風(fēng)格的類(lèi)型轉(zhuǎn)換
Widget *pw = new SpecialWidget;
update(pw); // 錯(cuò)誤!pw的類(lèi)型是Widget*,但是
// update函數(shù)處理的是SpecialWidget*類(lèi)型
update(const_cast<SpecialWidget*>(pw));
// 錯(cuò)誤!const_cast僅能被用在影響
// constness or volatileness的地方上。,
// 不能用在向繼承子類(lèi)進(jìn)行類(lèi)型轉(zhuǎn)換?br> 到目前為止,const_cast最普通的用途就是轉(zhuǎn)換掉對(duì)象的const屬性。
第二種特殊的類(lèi)型轉(zhuǎn)換符是dynamic_cast,它被用于安全地沿著類(lèi)的繼承關(guān)系向下進(jìn)行類(lèi)型轉(zhuǎn)換。這就是說(shuō),你能用dynamic_cast把指向基類(lèi)的指針或引用轉(zhuǎn)換成指向其派生類(lèi)或其兄弟類(lèi)的指針或引用,而且你能知道轉(zhuǎn)換是否成功。失敗的轉(zhuǎn)換將返回空指針(當(dāng)對(duì)指針進(jìn)行類(lèi)型轉(zhuǎn)換時(shí))或者拋出異常(當(dāng)對(duì)引用進(jìn)行類(lèi)型轉(zhuǎn)換時(shí)):
Widget *pw;
...
update(dynamic_cast<SpecialWidget*>(pw));
// 正確,傳遞給update函數(shù)一個(gè)指針
// 是指向變量類(lèi)型為SpecialWidget的pw的指針
// 如果pw確實(shí)指向一個(gè)對(duì)象,
// 否則傳遞過(guò)去的將使空指針。
void updateViaRef(SpecialWidget& rsw);
updateViaRef(dynamic_cast<SpecialWidget&>(*pw));
//正確。傳遞給updateViaRef函數(shù)
// SpecialWidget pw 指針,如果pw
// 確實(shí)指向了某個(gè)對(duì)象
// 否則將拋出異常
dynamic_casts在幫助你瀏覽繼承層次上是有限制的。它不能被用于缺乏虛函數(shù)的類(lèi)型上(參見(jiàn)條款M24),也不能用它來(lái)轉(zhuǎn)換掉constness:
int firstNumber, secondNumber;
...
double result = dynamic_cast<double>(firstNumber)/secondNumber;
// 錯(cuò)誤!沒(méi)有繼承關(guān)系
const SpecialWidget sw;
...
update(dynamic_cast<SpecialWidget*>(&sw));
// 錯(cuò)誤! dynamic_cast不能轉(zhuǎn)換
// 掉const。
如你想在沒(méi)有繼承關(guān)系的類(lèi)型中進(jìn)行轉(zhuǎn)換,你可能想到static_cast。如果是為了去除const,你總得用const_cast。
這四個(gè)類(lèi)型轉(zhuǎn)換符中的最后一個(gè)是reinterpret_cast。使用這個(gè)操作符的類(lèi)型轉(zhuǎn)換,其的轉(zhuǎn)換結(jié)果幾乎都是執(zhí)行期定義(implementation-defined)。因此,使用reinterpret_casts的代碼很難移植。
reinterpret_casts的最普通的用途就是在函數(shù)指針類(lèi)型之間進(jìn)行轉(zhuǎn)換。例如,假設(shè)你有一個(gè)函數(shù)指針數(shù)組:
typedef void (*FuncPtr)(); // FuncPtr is 一個(gè)指向函數(shù)
// 的指針,該函數(shù)沒(méi)有參數(shù)
// 返回值類(lèi)型為void
FuncPtr funcPtrArray[10]; // funcPtrArray 是一個(gè)能容納
// 10個(gè)FuncPtrs指針的數(shù)組
讓我們假設(shè)你希望(因?yàn)槟承┠涿畹脑颍┌岩粋€(gè)指向下面函數(shù)的指針存入funcPtrArray數(shù)組:
int doSomething();
你不能不經(jīng)過(guò)類(lèi)型轉(zhuǎn)換而直接去做,因?yàn)閐oSomething函數(shù)對(duì)于funcPtrArray數(shù)組來(lái)說(shuō)有一個(gè)錯(cuò)誤的類(lèi)型。在FuncPtrArray數(shù)組里的函數(shù)返回值是void類(lèi)型,而doSomething函數(shù)返回值是int類(lèi)型。
funcPtrArray[0] = &doSomething; // 錯(cuò)誤!類(lèi)型不匹配
reinterpret_cast可以讓你迫使編譯器以你的方法去看待它們:
funcPtrArray[0] = // this compiles
reinterpret_cast<FuncPtr>(&doSomething);
轉(zhuǎn)換函數(shù)指針的代碼是不可移植的(C++不保證所有的函數(shù)指針都被用一樣的方法表示),在一些情況下這樣的轉(zhuǎn)換會(huì)產(chǎn)生不正確的結(jié)果(參見(jiàn)條款M31),所以你應(yīng)該避免轉(zhuǎn)換函數(shù)指針類(lèi)型,除非你處于著背水一戰(zhàn)和尖刀架喉的危急時(shí)刻。一把鋒利的刀。一把非常鋒利的刀。
如果你使用的編譯器缺乏對(duì)新的類(lèi)型轉(zhuǎn)換方式的支持,你可以用傳統(tǒng)的類(lèi)型轉(zhuǎn)換方法代替static_cast, const_cast, 以及reinterpret_cast。也可以用下面的宏替換來(lái)模擬新的類(lèi)型轉(zhuǎn)換語(yǔ)法:
#define static_cast(TYPE,EXPR) ((TYPE)(EXPR))
#define const_cast(TYPE,EXPR) ((TYPE)(EXPR))
#define reinterpret_cast(TYPE,EXPR) ((TYPE)(EXPR))
你可以象這樣使用使用:
double result = static_cast(double, firstNumber)/secondNumber;
update(const_cast(SpecialWidget*, &sw));
funcPtrArray[0] = reinterpret_cast(FuncPtr, &doSomething);
這些模擬不會(huì)象真實(shí)的操作符一樣安全,但是當(dāng)你的編譯器可以支持新的的類(lèi)型轉(zhuǎn)換時(shí),它們可以簡(jiǎn)化你把代碼升級(jí)的過(guò)程。
沒(méi)有一個(gè)容易的方法來(lái)模擬dynamic_cast的操作,但是很多函數(shù)庫(kù)提供了函數(shù),安全地在派生類(lèi)與基類(lèi)之間進(jìn)行類(lèi)型轉(zhuǎn)換。如果你沒(méi)有這些函數(shù)而你有必須進(jìn)行這樣的類(lèi)型轉(zhuǎn)換,你也可以回到C風(fēng)格的類(lèi)型轉(zhuǎn)換方法上,但是這樣的話(huà)你將不能獲知類(lèi)型轉(zhuǎn)換是否失敗。當(dāng)然,你也可以定義一個(gè)宏來(lái)模擬 dynamic_cast的功能,就象模擬其它的類(lèi)型轉(zhuǎn)換一樣:
#define dynamic_cast(TYPE,EXPR) (TYPE)(EXPR)
請(qǐng)記住,這個(gè)模擬并不能完全實(shí)現(xiàn)dynamic_cast的功能,它沒(méi)有辦法知道轉(zhuǎn)換是否失敗。
我知道,是的,我知道,新的類(lèi)型轉(zhuǎn)換操作符不是很美觀而且用鍵盤(pán)鍵入也很麻煩。如果你發(fā)現(xiàn)它們看上去實(shí)在令人討厭,C風(fēng)格的類(lèi)型轉(zhuǎn)換還可以繼續(xù)使用并且合法。然而,正是因?yàn)樾碌念?lèi)型轉(zhuǎn)換符缺乏美感才能使它彌補(bǔ)了在含義精確性和可辨認(rèn)性上的缺點(diǎn)。并且,使用新類(lèi)型轉(zhuǎn)換符的程序更容易被解析(不論是對(duì)人工還是對(duì)于工具程序),它們?cè)试S編譯器檢測(cè)出原來(lái)不能發(fā)現(xiàn)的錯(cuò)誤。這些都是放棄C風(fēng)格類(lèi)型轉(zhuǎn)換方法的強(qiáng)有力的理由。還有第三個(gè)理由:也許讓類(lèi)型轉(zhuǎn)換符不美觀和鍵入麻煩是一件好事。