C++分析——常用技巧(二)

this指針

this指針,即指向自身類的指針,其不是類對象的一部分,不會影響類的空間大小(sizeof),this指針本身指向類對象自身的地址。

以下爲一個測試this指針的測試類。

class Test_this_Class
{
public:
    Test_this_Class(int a,int b):a(a),b(b){}
    Test_this_Class(const Test_this_Class & x):a(x.b),b(x.a){}  //複製構造函數賦值寫反
    ~Test_this_Class(){a = 0; b = 0;}
    int Read_Value_a(){return a;}
    int Read_Value_b(){return b;}
    void Set_Value_a(int a){this->a = a;}
    void Set_Value_b(int b){this->b = b;}
    const Test_this_Class & Comp_Class_Value(const Test_this_Class &C)const
    {
        if(C.a > this->a)
            return C;
        else 
            return *this;
    }
    void show()
    {
        std::cout << "a is " << this->Read_Value_a() << " b is " << this->Read_Value_b() << std::endl;
    }

private:
    int a;
    int b;
};

在讀函數Read_Value_a、Read_Value_b中,是最普遍的寫法,但代碼隱含了this指針,寫作 return this->a或return this->b也是可以的。this不僅能指向成員函數,也可以指向類的方法。

但函數有兩個地方必須用到this指針,即函數賦值的時候Set_Value_a、Set_Value_b和返回類本身的時候Comp_Class_Value,即
1、 當函數的傳參與成員函數同名時,其賦值一定要用this指針,
不能是 a = a;一定要this->a = a;
2、 當返回值是類本身的時候一定要用*this來代替其本身。

測試方法

    Test_this_Class this_1(4,7);
    Test_this_Class this_2(6,9);

    this_1.show();
    this_2.show();

    this_1.Set_Value_a(5);

    Test_this_Class this_3 = this_1.Comp_Class_Value(this_2);
    this_3.show();

執行結果

$ ./project.o 
a is 4 b is 7
a is 6 b is 9
a is 9 b is 6 
$

C++強制轉換

C++強轉不僅僅是像C一樣通過一個括號解決,雖然C++兼容C強轉的語法,但C++有自己的強轉語法
C強轉:(類型)參數變量
C++強轉:強轉關鍵字<類型>(參數變量)

和C強轉不一樣,參數變量中的括號不能省略,不然會編譯報錯。

測試代碼

int a = 10;
    float b = static_cast<float>a;

編譯報錯

module_CApp.cpp:180:30: error: expected ‘(’ before ‘a’
  float b = static_cast<float>a;
                        ^
module_CApp.cpp:180:31: error: expected ‘)’ before ‘;’ token
  float b = static_cast<float>a;
                         ^

正確寫法

    int a = 10;
    float b = static_cast<float>(a);

強轉關鍵字
1、 static_cast:基本數據類型之間的相互轉換,如int、float等
2、 const_cast:
3、 reinterpret_cast:指針類型的強轉
4、 dynamic_cast:指針類型的強轉

異常處理

C++異常處理關鍵字:try、catch、throw

try     :處理代碼
throw   :拋出異常
catch   :捕獲異常

拋出異常和C語言if else if判斷一樣,是每一步對結果的檢查,形式也和linux驅動註冊的goto寫法類似,同做爲異常處理的方法。

測試源碼

static void division_try_test(int y)
{
    int x = 9;
    float z = static_cast<float>(x)/static_cast<float>(y);  

    try
    {       
        if(y==0)
            throw string("y = 0 is error!");            //拋出字符串型異常
        else if(y < 0)
            throw string("y must be greater than 0!");  //拋出字符串型異常
        else if(y > 100)
            throw string("y must be less than 100!");       //拋出字符串型異常
        else if(y == 50)
            throw -1;                           //拋出整型異常,過濾特殊參數
        else if(y == 60)
            throw -2;                           //拋出整型異常,過濾特殊參數
        else if(y == 23)
            throw z;                            //拋出浮點型異常,過濾特殊參數

        cout << x << "/" << y << "=" << z << endl;
  }
  catch(string &error_msg)                  //接收字符串異常並處理
  { 
        cout << error_msg << endl;
  }
  catch(int &error_type)                    //接收整型異常並處理
  {
        cout << "error type is " << error_type << endl;
  }
  catch(...)                                //接收除上述以外的所有類型異常並處理
  { 
        cout << "Triggering an unknown error" << endl;
  }
}

最後一個寫法catch(…)和switch中的default很像,處理被忽略的異常,但catch(…)一定要放在末尾,不然會編譯不通過。

error: ‘...’ handler must be the last handler for its try block [-fpermissive]

測試方法

    division_try_test(2);
    division_try_test(0);
    division_try_test(-2);
    division_try_test(142);
    division_try_test(50);
    division_try_test(60);
    division_try_test(23);

測試結果

9/2=4.5
y = 0 is error!
y must be greater than 0!
y must be less than 100!
error type is -1
error type is -2
Triggering an unknown error

異常拋出後會強制結束try後面的代碼,與循環中的break相似。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章