簡體   English   中英

在其成員可訪問的 c++ 中創建 class

[英]Creating a class in c++ that is accessible by its members

我正在嘗試為模擬定義一個六邊形網格。 我正在使用立方體坐標,如此處所示: https://www.redblobgames.com/grids/hexagons/#neighbors-axis 我試圖使每個坐標(其中三個)成為 class 特定 object 命名的一部分。 例如,讓我們看看起源。 我有一個 class 持有三個坐標中的每一個,以及其他一些數據。 我希望將此 object 稱為 hex(0,0,0),這樣每個零都與 class 定義的三個坐標相關。 IE;


class hex{
     int r_coordinate: 
     int g_coordinate;
     int b_coordinate;
     metadata
     ...
}

hex hex(-2,1,1)={
     r_coordinate=-2;
     g_coordinate= 1;
     b_coordinate= 1;
     metadata
     ...
}

如果要創建 class hex對象並希望在創建過程中指定 RGB 值,則需要一個構造函數(也許有人編輯它以獲得更好的參考):

class hex {
    public:
        hex(int r, int g, int b)
        : r_coordinate(r), g_coordinate(g), b_coordinate(b)
        {
        }
    private:
        int r_coordinate;
        int g_coordinate;
        int b_coordinate;
};

hex x(1,2,3); // x.r_coordinate == 1, x.g_coordinate == 2, x.b_coordinate == 3

我不確定這是否是你所追求的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM