在向量中使用析构函数的类...? [英] class with destructor inside a vector...?

查看:72
本文介绍了在向量中使用析构函数的类...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到过以下奇怪的问题。

我用C ++编写代码并使用VC ++ 6编译器。


我有一个包含char * cstr作为私有成员的类strvector

我已经定义了它的析构函数,用于释放cstr保存的内存。


In main我创建了一个向量< strvector>

,每当我在这个向量中放置一个strvector对象时,程序

崩溃。


完整的程序如下

/////////////////////////////////// //////

#include< iostream.h>

#include" string"

#include" vector"


使用命名空间std;


class strvector

{

char * cstr ;


公开:


char * getcstr()

{

返回cstr;

}

strvector(char * s)

{

cstr = new char [strlen(s) )+1];

strcpy(cstr,s);

}

~strvector()

{

delete str ;

}

/ * release()

{

删除cstr;

} * /

};


void main()

{

vector< strvector> ; vec;


strvector str(one);

vec.push_back(str); //程序将崩溃

//str.release();


//cout<<vec[0].getcstr();//将打印垃圾值。

}

///////////////////////////////////
如果我删除析构函数并定义一个单独的成员函数

release以释放内存,cout语句将打印

garbase值。


有人请告诉我上面的程序出了什么问题

即使我遵循一个好的程序员方法。(使用析构函数

等)?


问候,

Yogesh Joshi

Hello all,
I have encountered with following strange problem.
I am coding in C++ and using VC++ 6 compiler.

I have a class strvector containing char * cstr as a private member
and i have defined its destructor for releasing memory hold by cstr.

In main i created a vector<strvector>
and whenever i put a object of strvector inside this vector the program
crashes.

The complete program is as follows
/////////////////////////////////////////
#include <iostream.h>
#include "string"
#include "vector"

using namespace std;

class strvector
{
char * cstr;

public:

char * getcstr()
{
return cstr;
}
strvector(char * s)
{
cstr = new char[strlen(s)+1];
strcpy(cstr,s);
}
~strvector()
{
delete str;
}
/*release()
{
delete cstr;
}*/
};

void main()
{
vector<strvector> vec;

strvector str("one");
vec.push_back(str);//The program will crash
//str.release();

//cout<<vec[0].getcstr();//will print a garbage values.
}
///////////////////////////////////
If i remove the destructor and define a separate member function
release for releasing the memory,the cout statement will print the
garbase value.

Will someone please tell me what is going wrong in the above program
even if i am following a good programmering approach.(using destructor
etc.)?

Regards,
Yogesh Joshi

推荐答案

yp*********@indiatimes.com 写道:
strvector(char * s)
{
cstr = new char [strlen(s)+1];
strcpy(cstr,s);
}
〜strvector()
{
删除str;


如果你是新的[],你需要删除[]。

}
strvector(char * s)
{
cstr = new char[strlen(s)+1];
strcpy(cstr,s);
}
~strvector()
{
delete str;
If you new[], you need to delete[].
}



此外,请阅读三个规则。


V


Also, read about "the Rule of Three".

V


>如果您是新的[],则需要删除[] 。

仍然它崩溃..我也试图把复制构造函数但没有

改进..仍然崩溃..代码只有当我删除
析构函数并且不会释放cstr持有的内存..


问候,

Yogesh

>If you new[], you need to delete[].
Still its crashing..Also i tried to put the copy constructor but no
improvement..still crashing..The code will run fine only when i remove
the destructor and will not release the memory held by cstr..

regards,
Yogesh


yp ********* @ indiatimes.com 写道:
如果你是新的[],你需要删除[]。
If you new[], you need to delete[].



仍然崩溃..此外,我试图把复制构造函数,但没有
改进..仍然崩溃..代码将运行良好只有当我删除析构函数,并不会释放由cstr持有的内存..

问候, Yogesh




三种状态的规则,你需要一个析构函数,复制构造函数

和赋值运算符。

如果仍然崩溃,则发布所有代码。在您的复制构造函数或赋值运算符的代码中,该错误几乎是



john



The rule of three states that you need a destructor, copy constructor
and assignment operator.

If it is still crashing then post all the code. The bug is almost
certainly in the code for your copy constructor or assignment operator.

john


这篇关于在向量中使用析构函数的类...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆