在閱讀類型轉(zhuǎn)換之前,還是先看一下關(guān)鍵字explicit(顯式)
書上的例子中的類名叫做Stack,這樣總感覺有些誤解,因為棧這個概念已經(jīng)在我們的腦海中先入為主了,容易形成思維定式.那么我寫一個和其他任何概念都無關(guān)的例子:
class MyClass
{
private:
int _number;
public:
MyClass(int number)
{
_number=number;
}
};
上面的類很普通,沒什么特別的.我們可以這樣創(chuàng)建這個類的對象:
int main()
{
MyClass instance=5;
}
這就比較有意思了,c++還能這樣寫,我以前的確沒有見過.那么explicit關(guān)鍵字的作用就是不讓你這樣寫,如果你這樣寫了,編譯器就會給你一個錯誤提示.現(xiàn)在改造一下MyClass
class MyClass
{
private:
int _number;
public:
explicit MyClass(int number)
{
_number=number;
}
};
main函數(shù)不變的情況下,編譯器會這樣提示你:
Explicit.cpp: In function ‘int main()’:
Explicit.cpp:14: 錯誤: conversion from ‘int’ to non-scalar type ‘MyClass’ requested
這樣,就只能這樣寫了:
MyClass instance(5);
好了,下面開始閱讀類型轉(zhuǎn)換的部分.
1 static_cast
這個操作符的執(zhí)行過程可以被認為是:它創(chuàng)建了一個新的對象,然后用要轉(zhuǎn)換的值去初始化這個新對象.這個轉(zhuǎn)換只有當一個類型轉(zhuǎn)換被聲明過才能執(zhí)行.
首先是一個簡單的例子:
//一個簡單的類型轉(zhuǎn)換
int valueToBeConverted=int();
using namespace std;
cout<<static_cast<float>(valueToBeConverted)<<"\n";
很像其他語言中的顯式類型轉(zhuǎn)換.那么對于我自己的類如何進行轉(zhuǎn)換呢?可以看一下下面的這個例子:
#include <iostream>
class MyClass2
{
public:
int IntMember1;
MyClass2()
{
IntMember1=1;
}
};
class MyClass1
{
public:
int IntMember1;
virtual void f()
{
std::cout<<IntMember1<<"\n";
}
explicit MyClass1(MyClass2 instance)
{
IntMember1=instance.IntMember1;
}
};
int main()
{
MyClass2 mc2Instance;
MyClass1 mc1Instance=static_cast<MyClass1>(mc2Instance);
mc1Instance.f();
}
注意看上面的粗體字explicit.如果沒有explict對于構(gòu)造函數(shù)的修飾,那么萬全可以簡單爽快的這么寫:
MyClass1 mc1Instance=mc2Instance;
換句話說,如果你想讓別人寫代碼的時候不關(guān)心類型,舒舒服服的寫出你認為不安全的代碼,那么你就去掉explicit,如果你想讓別人在寫代碼的時候,讓那些不安全的類型轉(zhuǎn)換變得更加"顯眼"一些,那么你就按上面這樣寫.然后讓編譯器狠狠的給他們一個警告.
2 dynamic_cast
This operator enables you to downcast a polymorphic type to its real static type. This is
the only cast that is checked at runtime. Thus, you could also use it to check the type of a
polymorphic value.
這個運算符能夠讓你把一個多態(tài)的類型轉(zhuǎn)換成它的真實的靜態(tài)類型.這是唯一的在運行時進行檢驗的轉(zhuǎn)換方法.所以你同樣需要在轉(zhuǎn)換之前檢驗多態(tài)值的類型.
首先dynamic_cast只能夠用于指針或者引用.否則編譯器會告訴你類似這樣的錯誤:
無法將 ‘scInstance’ 從類型 ‘SubClass’ 動態(tài)轉(zhuǎn)換到類型 ‘class BaseClass’ (target is not pointer or reference)
這里使用書中的例子:
class Car; // 至少擁有一個抽象函數(shù)的抽象類
class Cabriolet : public Car {

};
class Limousine : public Car {

};
void f(Car* cp)
{
Cabriolet* p = dynamic_cast<Cabriolet*>(cp);
if (p == NULL) {
//p并非Cabriollet類型

}
}
In this example, f() contains a special behavior for objects that have the real static type
Cabriolet. When the argument is a reference and the type conversion fails,
dynamic_cast throws a bad_cast exception (bad_cast is described on page 26).
Note that from a design point of view, it it always better to avoid such type-dependent
statements when you program with polymorphic types.
當參數(shù)是一個引用并且類型轉(zhuǎn)換失敗的時候,dynamic_cast會拋出一個bad_cast異常.值得注意的是,從設(shè)計的角度來說,通常在進行多態(tài)類型的程序編寫時,應(yīng)該避免這樣的類型依賴.
3 const_cast
This operator adds or removes the constness of a type. In addition, you can remove a
volatile qualification. Any other change of the type is not allowed.
這個操作符添加或除去一個類型的常量化(特性).另外,你可以除去一個變量變化的能力.任何對于這個類型變量的變化都將不被允許.
讓常量可修改的例子:
class MyClass
{
public:
int MemberValue;
void showValue()
{
cout<<"My Value is"<<MemberValue<<"\n";
}
MyClass()
{
MemberValue=1;
}
};
int main()
{
const MyClass myValue;
MyClass *ptr=const_cast<MyClass*>(&myValue);
ptr->MemberValue=100;
ptr->showValue();
}
上面的例子只是將一個const變得可以修改了,但是注意到書中的這樣一句:
In addition, you can remove a
volatile qualification. Any other change of the type is not allowed.
似乎還可以將一個可變量轉(zhuǎn)換為非可變的,這里沒有查到相關(guān)資料,也許書的后面會有相應(yīng)解釋,所以這里先放一放.
4 reinterpret_cast
這個比較有趣,支持不兼容類型之間的轉(zhuǎn)換.以下是例子:
#include <iostream>
using namespace std;
class ClassA
{
public:
int Value1;
int Value2;
int Value3;
void showValue()
{
cout<<Value1<<","<<Value2<<","<<Value3<<"\n";
}
};
class ClassB
{
public:
int Value1;
int Value2;
int Value3;
ClassB()
{
Value1=1;
Value2=2;
Value3=3;
}
};
int main()
{
ClassB *b=new ClassB();
ClassA *a=reinterpret_cast<ClassA*>(b);
a->showValue();
}
同樣,這種轉(zhuǎn)換對于非基本類型來說,仍然只能支持指針和引用.
這些運算符的優(yōu)點就是他們明確了類型轉(zhuǎn)換的意圖,并且能夠讓編譯器獲取更多的信息來了解這些類型轉(zhuǎn)換的原因,然后當這些轉(zhuǎn)換操作越過他們的職能的時候,編譯器會報告錯誤.
另外需要注意的是,這些類型轉(zhuǎn)換都只能接受一個參數(shù).看下面的例子:
static_cast<Fraction>(15,100)
上面不會產(chǎn)生你想要的結(jié)果.這里的逗號并不是用來連接兩個參數(shù)的,而是作為逗號運算符出現(xiàn),這里15會被舍棄,而采用100作為參數(shù)構(gòu)造Fraction.
所以還是需要采用下面的方法才合適:
Fraction(15,100)
posted on 2007-06-17 14:56
littlegai 閱讀(267)
評論(0) 編輯 收藏 引用 所屬分類:
我的讀書筆記