搜尋此網誌

2011年11月22日 星期二

C++ 型別轉換


C++ 的型別轉換有以下四種轉換方式 分別是 static_cast, dynamic_cast,reinterpret_cast 以及const_cast .分述如下:

static_cast
可用於轉換基底類別指標為衍生類別指標,也可用於傳統的資料型態轉換。
舉例來說,在指定的動作時,如果右邊的數值型態比左邊的數值型態型態長度大時,
超出可儲存範圍的部份會被自動消去,例如將浮點數指定給整數變數,則小數的部份會被自動消去
Ex:
int num1 = 0;
double num2 = 1.11;
num1 = static_cast<int>(num2);
dynamic_cast
使用static_cast(甚至是傳統的C轉型方式)將基底類別指標轉換為衍生類別指標,
這種轉型方式稱為強制轉型,但是在執行時期使用強制轉型有危險性,
因為編譯器無法得知轉型是否正確.
Ex:
#include <iostream>
struct Base {
    virtual ~Base() {}
    virtual void name() {}
};
struct Derived: Base {
    virtual ~Derived() {}
    virtual void name() {}
};

struct Some {
    virtual ~Some() {}
};

int main()
{
    Some *s = new Some;
    Base* b1 = new Base;
    Base* b2 = new Derived;

    Derived *d1 = dynamic_cast<Derived*>(b1);
    Derived *d2 = dynamic_cast<Derived*>(b2);
    Derived *d3 = dynamic_cast<Derived*>(s);
    Base *d4 = dynamic_cast<Base*>(s);

    std::cout << "'b1' points to 'Derived'? : " << (bool) d1 << '\n';
    std::cout << "'b2' points to 'Derived'? : " << (bool) d2 << '\n';
    std::cout << "'s' points to 'Derived'? : " << (bool) d3 << '\n';
    std::cout << "'s' points to 'Base'? : " << (bool) d4 << '\n';
}

Output:
i = 4
type::i = 4
'b1' points to 'Derived'? : false
'b2' points to 'Derived'? : true
's' points to 'Derived'? : false
's' points to 'Base'? : false 
reinterpret_cast
用於將一種型態的指標轉換為另一種型態的指標,例如將char*轉換為int*
Ex:
#include  <iostream>
using namespace std;
int main() {
    int i;
    char* str = "test";
    i = reinterpret_cast(str);
    cout << i << endl;
    return 0;
}
const_cast
用於一些特殊場合可以覆寫變數的const屬性,利用cast後的指標就可以更改變數的內部。
const_cast是一危險的轉型"唯一使用"的場合是當物件資料屬性為const時但有時卻必須傳遞給非const參數的成員函數或一般全域函數時才可用它來移除const屬性因為唯有如此才可
符合型態相同(形式引數(參數))但使用時必須確定此轉型後不會藉此來更改其資料否則可能會造成無法預測的結果!!!
Ex:
#include <iostream>
struct type {
    type():i(3)
    {}

    void m1(int v) const 
    {
       const_cast<type*>(this)->i = v;
    }
    int i;
};
 
int main() 
{
    type t;
    t.m1(4);
    std::cout << "type::i = " << t.i << '\n';
}

Output:
type::i = 4

2011年11月4日 星期五

如何紀錄一個矩形區塊(rb)在一個矩形範圍(ra)的相對位置 ,並依比例還原至絕對位置及範圍

A. 紀錄相對位置及範圍
1. 利用 QPointF 分別紀錄rb在ra當中的左上角的點,與右下角的點的相對位置
ex:

   // 假設變數定義如下
   // s_topleft_x  (rb 左上角x在ra的絕對位置)
   // s_topleft_y  (rb 左上角y在ra的絕對位置)
   // s_bottomright_x  (rb 右下角x在ra的絕對位置)
   // s_bottomright_y   (rb 右下角y在ra的絕對位置)
   // picWidth    (ra的寬)
   // picHeight   (ra的高)
   
   QPointF topPF = QPointF((double)s_topleft_x/picWidth, (double)s_topleft_y/picHeight);
     QPointF bottomPF = QPointF((double)s_bottomright_x/picWidth, (double)s_bottomright_y/picHeight);
   
2. 設定 QRectF
ex:
    QRectF newRect(topPF, bottomPF);
   
   
以上newRect就記錄了rb在ra中的相對位置及範圍了


B.還原相對位址

  // 假設變數定義如下
  // newPicWidth  (欲還原ra的寬)
  // newPicHeight (欲還原ra的高)
  // newRect (之前紀錄的rb的相對位置的矩形範圍) 
 
  QRect mRect;
    mRect.setX((int)(((double)newPicWidth)* newRect.x()));
    mRect.setY((int)(((double)newPicHeight)* newRect.y()));
    mRect.setBottom((int)(((doublenewPicHeight)* newRect.bottom()));
    mRect.setRight((int)(((double)newPicWidth)* newRect.right()));
 
以上 mRect 就是 rb在新的ra當中的絕對位置及範圍了