1

私は次のコードを持っています。

class Wave {

int m_length;
data_type * m_data;

public:

    Wave(){
        blah...blah...blah
        m_data = NULL;
        m_length = 0;
        cout << "Wave " << this << " created on " << m_data << " with m_length " <<  m_length << endl;
    }

    Wave(int len, data_type data){
        blah...blah...blah
        if (len) {
            m_length = len;
            m_data = new data_type [m_length];
        } else {
            m_length = 0;
            m_data = NULL;
        }
        cout << "Wave " << this << " created on " << m_data << " with m_length " <<  m_length << endl;
    }



    ~Wave() 
    {
    cout << "Wave " << this << " destructor  on " << m_data << " started ";
        if (m_length) delete[] m_data;
    cout << "and finished " << endl;
    };        





    Wave & operator+= (const Wave wave){
    cout << __FUNCTION__ << ":" << __LINE__ << " m_length " << m_length << endl;
    if (NULL != m_data){
        data_type * tBuf = new data_type [m_length + wave.Length()];
        copy (wave.begin(),wave.end(), copy (begin(),end(),iterator(tBuf)));
        cout << "Wave " << this << " data on " << m_data << " moved onto " << tBuf;
        delete[] m_data;
        m_data = tBuf;
        cout << " and deleted" << endl;
    } else {
        m_data = new data_type [wave.Length()];
        copy (wave.begin(), wave.end(), begin());
        cout << "Wave " << this << " data created on " << m_data << " of length " <<  wave.Length() << endl;
    }
    m_length += wave.Length();
    cout << __FUNCTION__ << ":" << __LINE__ << " m_length " << m_length << endl;
    return *this;
    };

}


main(){

blah..blah..blah

Wave sample;

for (......) {

    cout << pulseNum << "-th High part: " << pulse->first << endl;
    Wave tSample(x,y);
    blah.blah.blah

    sample += tSample;

    cout << endl << pulseNum++ << "-th Low part: " << pulse->second << endl;
    tSample = Wave(a,b);
    blah.blah.blah

    sample += tSample;
}

}

以下は、このコードの実行ログです

Wave 0x28fe34 created on 0 with m_length 0
0-th High part: 220
Wave 0x28fe54 created on 0xc60f00 with m_length 207
operator+=:211 m_length 0
Wave 0x28fe34 data created on 0xc610a8 of length 207
operator+=:230 m_length 207
Wave 0x28fe9c destructor  on 0xc60f00 started and finished

0-th Low part: 320
Wave 0x28febc created on 0xc61250 with m_length 301
Wave 0x28febc destructor  on 0xc61250 started and finished
operator+=:211 m_length 207
Wave 0x28fe34 data on 0xc610a8 moved to 0xc61250 and deleted 
operator+=:230 m_length 508
Wave 0x28fee0 destructor  on 0xc61250 started and finished

Wave 0x28fe54 destructor  on 0xc61250 started and finished

私にとって最も奇妙なことは、デストラクタがコンストラクタよりも何度も呼び出されることです。さらに、これまでに構築されたことのないオブジェクトに対して、同じデータアドレスに対して呼び出されました。
どうしてそれができますか?

4

2 に答える 2

8

Waveコンパイラによって生成されたコピーコンストラクタがありますが、出力は表示されません。

このコピーコンストラクタは、たとえば、のパラメータであるオブジェクトを構築するために呼び出されますWave & operator+= (const Wave wave)

于 2012-10-18T11:25:08.770 に答える
2

コードを簡素化したい場合は、std::vector<data_type> m_data生のポインター ( ) を使用する代わりに、データ メンバーを定義するだけdata_type * m_dataです。

このようにして、コンパイラは適切なコピー コンストラクタ、copy operator= (および C++11 準拠コンパイラのムーブ セマンティクス) およびクラスのデストラクタを自動的に生成します (自動的に生成されたコピー コンストラクタ、copy operator= およびデストラクタはメンバーを操作しますたとえば、自動的に生成されたコピー コンストラクターは、クラスの各データ メンバーに対してコピー コンストラクターを呼び出します)。

データを割り当てるには、次の代わりに:

m_data = new data_type [m_length];

使用する:

m_data.resize(m_length);

は独自のサイズを知っているm_lengthため、データ メンバーを取り除くこともできます (メソッドを介してアクセスできます)。std::vectorstd::vector::size()

std::vectorすべての要素を 1 つの連続したメモリ領域 ( などnew[])に格納することに注意してください。を使用してその領域の先頭にアクセスできます&m_data[0]

于 2012-10-18T11:33:26.390 に答える