2018年6月16日 星期六

封裝的進階議題

Nest Classes巢狀類別
class ff{
.
.
.
private: //Nest Classes
class oo{..}
}

  • 內部類別不能存取外部類別的private成員,外部類別亦不能存取內部的private成員
  • ->若要存取,必須宣告其為friend
  • 類別定義時,需要實做內部以及外部
  • 使用者呼叫時,不需要知道內部成員是誰

// 實作內部類別
PointDemo::Point::Point() {
    _x = 0;
    _y = 0;
}

// 實作內部類別
PointDemo::Point::Point(int x, int y) {
    _x = x;
    _y = y;
}
Local Classes區域類別
類別也可以定義於函式之中,稱之為區域類別(Local classes)
區域類別不可以直接存取所在函式中的變數可以存取外圍類別的私用成員
#include 
using namespace std;


int main() {
    // 區域類別
    class Point {
    public:
        int x;
        int y;
        Point(int x, int y) {
            this->x = x;
            this->y = y;
        }
    };
 
    Point p(10, 10);

    cout << "(x, y) = (" 
         << p.x << ", " 
         << p.y << ")" 
         << endl;
 
    return 0;
}

沒有留言:

張貼留言

Ethereum- Learn Solidity step by step

Common Function Types: public: Anyone can call this function,but it isn't really used for any type of security per se. priv...