缺少析构函数 [英] Missing Destructor

查看:100
本文介绍了缺少析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一系列对象,在程序执行过程中我会给b $ b分配一个特定对象到对象中的某个元素

数组。示例代码'是这样的...


class ClassA

{

public:

ClassA()

:m(rand()%173){cout<<" \ nRandom构造函数:"<< m; }


ClassA(int val)

:m(val){cout<<" \\\
Constructed:"<< m; }


~ClassA(){cout<<" \ n\\\
Destructor:"<< m; }

私人:

int m;

};


int main()

{

srand((无符号)时间(NULL));


const int size = 3;

ClassA * c = new ClassA [size];


c [0] = ClassA(200);

c [0] = ClassA(201);


删除[] c;

返回0;

}


/ /输出......

随机构造函数:50< ---(对象消失,不带析构函数调用)

随机构造函数:38

随机构造函数:148

构造:200


析构函数:200

构造:201


析构函数:201< ---(1.第一个析构函数调用此对象)


析构函数:148


析构函数:38


析构函数:201< ---(2.对象被销毁两次或者它已经取消了第一个对象?
取代了第一个对象?)


正如我在输出中指出的那样,第一个的析构函数

对象永远不会被调用,而创建对象的析构函数

,data = 201会被调用两次。是这种情况还是非常实际的第二个对象,它实际上是在(2)中被摧毁了?


另外,什么是最好的这种情况的实践是什么?

Hi All,

I''ve got an array of objects, during the execution of the program I''d
like to assign a particular object to a certain element in the object
array. The sample code''s like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned)time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I''ve pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what''s the best practice for such a situation?

推荐答案



" AB" < AB ***** @ gmail.com>在消息中写道

news:11 ********************* @ i40g2000cwc.googlegro ups.com ...

"AB" <ab*****@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
大家好,

我有一个对象数组,在程序执行期间我想要将特定对象分配给某个元素。对象
数组。示例代码'是这样的...

类ClassA
{
公开:
ClassA()
:m(rand()% 173){cout<<" \ nRandom构造函数:"<< m; } A />
ClassA(int val)
:m(val){cout<<" \\\
Constructed:"<< m; }

~ClassA(){cout<<" \ n\\\
Destructor:"<< m; }
私有:
int m;
};

int main()
{/ / srand((无符号)时间(NULL) );

const int size = 3;
ClassA * c = new ClassA [size];

c [0] = ClassA(200);
c [0] = ClassA(201);

删除[] c;
返回0;
}
//输出..随机构造函数:50< ---(对象消失,没有析构函数调用)
随机构造函数:38
随机构造函数:148
构造:200
构造:201
析构函数:201< ---(1.第一个析构函数调用此对象)

析构函数:148

析构函数:38

析构函数:201< ---(2.对象被销毁两次或者它已经取代了第一个对象?)正如我在输出中指出的那样,第一个
对象的析构函数永远不会被调用,而用data = 201创建的对象的析构函数会被调用两次。是这种情况还是第一个实际上在(2)中被摧毁的对象?

此外,这种情况的最佳实践是什么?
Hi All,

I''ve got an array of objects, during the execution of the program I''d
like to assign a particular object to a certain element in the object
array. The sample code''s like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned)time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I''ve pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what''s the best practice for such a situation?




我只能说我在MS VC ++中看到了相同的行为.net 2003




All I can say is I see the same behavior in MS VC++ .net 2003





AB写道:

AB wrote:
大家好,

我有一个对象数组,在执行程序期间我''
喜欢将特定对象分配给对象
数组中的某个元素。示例代码'是这样的...

类ClassA
{
公开:
ClassA()
:m(rand()% 173){cout<<" \ nRandom构造函数:"<< m; } A />
ClassA(int val)
:m(val){cout<<" \\\
Constructed:"<< m; }

~ClassA(){cout<<" \ n\\\
Destructor:"<< m; }
私有:
int m;
};

int main()
{/ / srand((无符号)时间(NULL) );

const int size = 3;
ClassA * c = new ClassA [size];

c [0] = ClassA(200);
c [0] = ClassA(201);

删除[] c;
返回0;
}
//输出..随机构造函数:50< ---(对象消失,没有析构函数调用)
随机构造函数:38
随机构造函数:148
构造:200
构造:201
析构函数:201< ---(1.第一个析构函数调用此对象)

析构函数:148

析构函数:38

析构函数:201< ---(2.对象被销毁两次或者它已经取代了第一个对象?)正如我在输出中指出的那样,第一个
对象的析构函数永远不会被调用,而用data = 201创建的对象的析构函数会被调用两次。是这种情况还是第一个实际上在(2)中被摧毁的对象?

此外,这种情况的最佳实践是什么?
Hi All,

I''ve got an array of objects, during the execution of the program I''d
like to assign a particular object to a certain element in the object
array. The sample code''s like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned)time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I''ve pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what''s the best practice for such a situation?




类似
的声明
c [0] = ClassA(200);

构造一个临时对象并将其分配给C [0]。在

语句之后,临时对象被破坏而不是c [0]。这就是你看到200架构造和破坏的原因。它们指的是temp

对象而不是c [0]。



A statement like
c[0] = ClassA(200) ;
constructs a temporary object and assigns it to c[0]. After the
statement the temporary object is destructed and not c[0]. That is the
reason you see contructed and destructed for 200. They refer to temp
object rather than c[0].


* AB:
大家好,

我有一个对象数组,在程序执行期间我'' d
喜欢将特定对象分配给对象
数组中的某个元素。示例代码'是这样的...

类ClassA
{
公开:
ClassA()
:m(rand()% 173){cout<<" \ nRandom构造函数:"<< m; } A />
ClassA(int val)
:m(val){cout<<" \\\
Constructed:"<< m; }

~ClassA(){cout<<" \ n\\\
Destructor:"<< m; }
私有:
int m;
};

int main()
{/ / srand((无符号)时间(NULL) );

const int size = 3;
ClassA * c = new ClassA [size];

c [0] = ClassA(200);
c [0] = ClassA(201);

删除[] c;
返回0;
}
//输出..随机构造函数:50< ---(对象消失,没有析构函数调用)
随机构造函数:38
随机构造函数:148
构造:200
构造:201
析构函数:201< ---(1.第一个析构函数调用此对象)

析构函数:148

析构函数:38

析构函数:201< ---(2.对象被销毁两次或者它已经取代了第一个对象?)正如我在输出中指出的那样,第一个
对象的析构函数永远不会被调用,而用data = 201创建的对象的析构函数会被调用两次。是这种情况还是第一个实际上在(2)中被摧毁的对象?

此外,这种情况的最佳实践是什么?
Hi All,

I''ve got an array of objects, during the execution of the program I''d
like to assign a particular object to a certain element in the object
array. The sample code''s like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned)time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I''ve pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what''s the best practice for such a situation?




我称之为C ++构造保证:除了使用非常好的
低级别功能外,每个对象只接收一个构造函数调用

(构建完所有子对象之后),这是对象发生的第一件事,对于每个构造函数调用,最多只有

一个析构函数调用 - 除非你没有销毁一个对象,否则每个构造函数调用都会有一个析构函数调用。


上面的代码使用了两种技术让它不那么明显发生了什么:


*改变对象表面身份的值赋值。


*临时对象。


你第一个析构函数调用报告ID 201是第一个数组

元素,se cond是临时对象,具有相同的id。


要研究构造函数和析构函数调用,你需要的代码是

易于分析而不是代码带来各种各样的效果。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



What I call the "C++ construction guarantee": except for use of very
low-level features each object receives exactly one constructor call
(after all subobjects constructed), which is the first thing that
happens for that object, and for each constructor call there is at most
one destructor call -- unless you fail to destroy an object there is
exactly one destructor call for each constructor call.

The code above uses two techniques to make it less apparent what''s going on:

* Value assignment that changes the apparent identity of an object.

* Temporary objects.

You first destructor call reporting id 201 is of the first array
element, the second is of the temporary object, which has the same id.

To investigate constructor and destructor calls you need code that is
easy to analyze instead of code that brings in all kinds of effects.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于缺少析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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