簡體   English   中英

如何使用boost :: dynamic_bitset為類重載()運算符

[英]How to overload () operator for a class using boost::dynamic_bitset

我創建了一個類,可以從位集中模擬2D數據集。 到目前為止,這是我創建的內容:

class Data
{
    private: 
        int width_; 
        int height_; 
        boost::dynamic_bitset<> * bitset_; dynamicznie

    public:
        Data() : width_(0), height_(0) {} 
        Data(int width,  int height) : width_(width), height_(height) 
        {
            bitset_ = new boost::dynamic_bitset<>(width * height); 
        }

    boost::dynamic_bitset<>::reference & operator()(const int x, const int y)
    {
        return bitset_[x * height_ + y];
    }

};

不幸的是,這不起作用,它指示錯誤:

boost::dynamic_bitset<>::reference & operator()(const int x, const int y)
    {
        return bitset_[x * height_ + y];
    }

我希望能夠在此類的實例中操縱位集:

Data data = new Data(10, 10);
bool i = 1
data(0, 1) = i;

顯然,dynamic_bitset本身提供了一個operator []。 您已將其聲明為指針,因此將方括號視為該指針的索引訪問; 它將您的指針視為指向數組的指針,並返回該數組的元素(不存在和未分配),該元素是對dynamic_bitset的(懸掛)引用,而不是對單個位的引用。

是否有真正的理由將該成員用作指針? 您甚至不以復制/轉讓/銷毀方式來處理它,因此違反了五項原則。 只需使其成為對象成員,而不是指向堆上分配的對象的指針即可:

    boost::dynamic_bitset<> bitset;
public:
    Data(int w, int h): width_(w), height_(h), bitset(w * h) {}

PS std::size_t如果不是真正使用負尺寸,則更std::size_t作為尺寸尺寸。

bitset_是一個指針,使用前應先取消引用

    (*bitset_)[x * height_ + y];

並記住, dynamic_bitset operator[]返回臨時對象,

參考運算符[](size_type pos);

bool operator [](size_type pos)const;

您不能將其分配給L值參考dynamic_bitset<>::reference& 因此,您應按以下方式定義您的運算符()

boost::dynamic_bitset<>::reference  operator()(const int x, const int y)
{
    return (*bitset_)[x * height_ + y];
}

我無法發布其他問題,因此我將在此處發布。 我想了解從IFS函數生成分形的算法。 該算法代碼位於此處: http : //www.algorytm.org/fraktale/system-funkcji-iterowanych-ifs/ifs-c.html但我不明白whast是minX,minY,maxX,maxY的目的, ratioX,ratioY變量。 我就是做不到 我已經想了很多,想不通。

有趣的是,這些格線必須設置為特定值。 否則,算法將無法正常工作(我已經對此進行了測試)。

暫無
暫無

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

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