私はocted_stringという名前のクラスを作成しましたが、デストラクタはうまく動作しませんでしたが、関数でocted_string型を返すことができませんでした。C++のデストラクタについて
下記のコードを見て、何が間違っているか教えてください。
私はそれが動作するデストラクタを削除します! (cout print 12)
いずれかの助けができますか?
octed_string(const octed_string &_input)
:value(0),length(0),allocated(0)
{
while(length<_input.length)
{
this->add((uchar_t)_input[length]);
}
}
octed_string& octed_string::operator= (octed_string const& _in)
{
octed_string tmp(_in);
return *this;
}
:
class octed_string
{
private:
uint8_t *value;
size_t length;
size_t allocated;
public:
octed_string()//constructor
:value(0),length(0),allocated(0)
{
}
void copy(uint8_t *from, uint8_t *to)
{
for (size_t i = 0; i < length; i++)
*to++ = *from++;
}
void allocate()
{
if (value == 0)
{
allocated = STACK_INITIAL_ALLOC;
value = new uint8_t[allocated];
}
else
{
// We need to allocate more memory
size_t new_allocated = allocated + STACK_CHUNK_ALLOC;
uint8_t *new_value = new uint8_t[new_allocated];
// Copy from old stack to new stack
copy(value, new_value);
// Delete the old value
delete [] value;
allocated = new_allocated;
value = new_value;
}
}
~octed_string()//destructor
{
if(value)
delete [] value;
}
friend ostream &operator<<(ostream &_output,const octed_string &_str)//opration overloading for cout
{
for(int i=0;i<_str.length;i++)
_output<<(uchar_t)_str.value[i];
return _output;
}
void add(uint8_t input)//this function automatically add space to value (new)
{
if (length == allocated)
allocate(); // Allocate more memory
value[length++] = _value;
}
octed_string sub_string(int start,int end)//(this function has a problem with destructor i think because it return octed_string)
{
octed_string a;
for(int i=start;i<end;i++)
a.add(a);
return a;
}
};
void main()
{
octed_string o; //object
o.add(1);
o.add(2);
o.add(3);
cout<<o.sub_string(0,2); //i expect printing 12 but i does not!
}
----------------------- //答え おかげで、次のコードを追加することによって、それが修正phresnelします
しかし、私はまだ理由を理解できません。どのような身体もこの問題を学ぶための参考資料を提示することができますか?
問題がおそらくそこにあるので、あなたはadd()にあるものを正確に表示する必要があると思います。 – Julian
あなたは最も重要なコードを逃したようです: 'add'メソッド。 –
これは実際にあなたのコードのすべてですか?あなたのadd関数は何もしません。 –