在同一成员上调用函数使用访问函数与使用成员函数产生不同的结果

时间:2017-05-02 03:10:38

标签: c++ function reference member

我使用访问功能通过引用访问成员数据,mslc,1),以及2)直接使用其他成员函数。我显示成员数据的内容,一个bitset,它根据我使用的方法显示不同。它不应该是这种方式,因为我每次都访问相同的成员数据。我错过了什么?这是输出:

成员slc:
0 1 0 0 0 0 0 0 0 0 0 0
水库:
这里的mslc:
1 0 1 0 0 1 0 1 0 1 0 0

两位模式应该相同。他们应该是第一个。

class Tdoh // contains a bitset - includes other things not at issue here
{
private:
    Tpiz mslc; // the bitset
public:
    Tpiz& get_mslc();
    int res(); // displays the mslc bit pattern
};
Tpiz& Tdoh::get_mslc()
{
    return mslc;
}
void Tdoh::res()
{
    cout << "Here's mslc:" << endl;

    for(int m=0;m<=11;m++)
    {
        cout << mslc.get_setPiz_elem(m) << " "; // show bit pattern of member mslc
    }
    cout << endl;
}
class Tpiz
{
    private:
        bitset<12> setPiz;
    public:
        void set_setPat(bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool); // sets the bit pattern
        bool get_setPiz_elem(int) const; // gets the designated bit
};
int main(int argc, char** argv) 
{
    Tdoh doh; // object contains a member bitset
    Tpiz temp2_piz; // a separate bitset
    temp2_piz = doh.get_mslc(); // get a reference to the object's member bitset
    temp2_piz.set_setPat(0,1,0,0,0,0,0,0,0,0,0,0); // set the member bitset's pattern
    cout << "member slc:" << endl;
    for(int j=0;j<=11;j++)
    {
        cout << temp2_piz.get_setPiz_elem(j) << " "; // display the member bitset's pattern
    }
    cout << endl; 
    cout << "res:" << endl; 
    cout << doh.res() << endl; // see function code. It accesses member bitset directly and displays pattern, but 
    // in this case the pattern is wrong because it's not the pattern 010000000000 that was set above. Instead, it's a
    // different, default pattern set when a Tpiz is created, before it is set by something like set_setPat(0,1,0,0,0,0,0,0,0,0,0,0).
    return 0;
}

0 个答案:

没有答案