他の人が書いた古いプロジェクトのバグを修正しようとしています。以下は、私が現在取り組んでいるコードの一部です。
共用体でのポインターの使用法について明確でないことがいくつかあります。ユニオン内のポインタが指すメモリはどのように解放できますか? ポインタにメモリを割り当てる必要がありますか? また、それらはコードの他の問題だと思います。親切に指摘してください。どうもありがとう!
コード:
class A
{
long filePos;
union EU
{
float *recording;
UINT64 timeStamp;
EU(): timeStamp((UINT64)0) //should I allocate memory here?
{
}
EU(const EU& eu)
{
if(eu.timeStamp) //is this the way to check which field of union is used currently?
timeStamp = eu.timeStamp;
else
recording = eu.recording;
}
EU& operator=(const EU& eu)
{
if(this == &eu)
return *this;
if(eu.timeStamp)
timeStamp = eu.timeStamp;
else
recording = eu.recording;
return *this;
}
EU(UINT64 ts):timeStamp(ts)
{
}
~EU()
{
}
}EU;
public :
inline A(long fpos, UINT64 ts) :filePos(fpos),EU(ts)
{
}
inline A(const EDFItem & ei)
{
filePos = ei.filePos;
EU=ei.EU;
}
~A ()
{
}
inline A& operator=(const A& ei)
{
if(this == &ei)
return *this;
filePos = ei.filePos;
EU=ei.EU;
return *this;
}
}