显式调用构造函数和析构函数 [英] explicit call of constructor and destructor

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

问题描述

大家好,


我有以下代码,我可以明确地调用析构函数

而不是构造函数。当我调用构造函数时,我得到一个编译时错误

,为什么会这样呢?


class Trial

{

public:Trial()

{

printf(" Trial \ nn);;

}

~试用()

{

printf("〜Trial\ n);

} < br $>
};


int main()

{

试用对象;

obj.Trial(); //导致编译时错误

obj。〜Trial();

}

Hi Everyone,

I have the following code and i''m able to invoke the destructor
explicitly but not the constructor. and i get a compile time error
when i invoke the constructor, why is this so?

class Trial
{
public: Trial()
{
printf("Trial\n");
}
~Trial()
{
printf("~Trial\n");
}
};

int main()
{
Trial obj;
obj.Trial(); // Causes compile time error
obj.~Trial();
}

推荐答案

12月3日晚上9:55,Rahul< sam _... @ yahoo.co.inwrote:
On Dec 3, 9:55 pm, Rahul <sam_...@yahoo.co.inwrote:

我有以下代码,我是能够明确地调用析构函数

而不是构造函数。当我调用构造函数时,我得到一个编译时错误

,为什么会这样呢?


class Trial

{

public:Trial()

{

printf(" Trial \ nn);;

}

~试用()

{

printf("〜Trial\ n);

} < br $>

};


int main()

{

试用obj;

obj.Trial(); //导致编译时错误

obj。〜Trial();

I have the following code and i''m able to invoke the destructor
explicitly but not the constructor. and i get a compile time error
when i invoke the constructor, why is this so?

class Trial
{
public: Trial()
{
printf("Trial\n");
}
~Trial()
{
printf("~Trial\n");
}

};

int main()
{
Trial obj;
obj.Trial(); // Causes compile time error
obj.~Trial();



两次调用析构函数是未定义的行为。唯一的地方

显式调用析构函数是必需的并且证明是合理的是

当你使用placement new来构造函数而不是预先分配的

缓冲区。新的位置可以被视为对构造函数的显式调用

,因为它实际上就是这样。

Calling the destructor twice is undefined behavior. The only place
where explicitly calling the destructor is required and justified is
when you used placement new to do the constructor over a pre-allocated
buffer. The placement new can be considered as being an explicit call
to the constructor as that is what it actually does.


Rahul写道:
Rahul wrote:

大家好,


我有以下代码,我可以调用析构函数

显式但不是构造函数。当我调用构造函数时,我得到一个编译时错误

,为什么会这样呢?


class Trial

{

public:Trial()

{

printf(" Trial \ nn);;

}

~试用()

{

printf("〜Trial\ n);

} < br $>
};


int main()

{

试用对象;

obj.Trial(); //导致编译时错误
Hi Everyone,

I have the following code and i''m able to invoke the destructor
explicitly but not the constructor. and i get a compile time error
when i invoke the constructor, why is this so?

class Trial
{
public: Trial()
{
printf("Trial\n");
}
~Trial()
{
printf("~Trial\n");
}
};

int main()
{
Trial obj;
obj.Trial(); // Causes compile time error



正确。在此之前的行调用您的构造函数。什么

你想在这里做什么?

Correct. The line before this one invokes your constructor. What
are you trying to do here?


obj。〜Trial();
obj.~Trial();



当''main''函数结束时,这将导致未定义的行为。

本地obejct''obj'的析构函数将再次调用。

为什么要在这里调用析构函数?

This will cause undefined behaviour when your ''main'' function ends.
The destructor for the local obejct ''obj'' will be called again.
Why are you trying to invoke the destructor here?


}
}



V

-

请在通过电子邮件回复时删除资金''A'

我这样做请回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


12月3日晚上10点06分,Abhishek Padmanabh< abhishek.padman ... @ gmail .com>

写道:
On Dec 3, 10:06 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

12月3日晚上9:55,Rahul< sam _... @ yahoo.co。 inwrote:
On Dec 3, 9:55 pm, Rahul <sam_...@yahoo.co.inwrote:

我有以下代码,我可以明确地调用析构函数

而不是构造函数。当我调用构造函数时,我得到编译时错误

,为什么会这样?
I have the following code and i''m able to invoke the destructor
explicitly but not the constructor. and i get a compile time error
when i invoke the constructor, why is this so?


class Trial

{

public:Trial()

{

printf(" Trial \ nn);

}

~试用()

{

printf("〜Trial\ n);

}
class Trial
{
public: Trial()
{
printf("Trial\n");
}
~Trial()
{
printf("~Trial\n");
}


};
};


int main()

{

试用obj;

obj.Trial(); //导致编译时错误

obj。〜Trial();
int main()
{
Trial obj;
obj.Trial(); // Causes compile time error
obj.~Trial();



两次调用析构函数是未定义的行为。唯一的地方

显式调用析构函数是必需的并且证明是合理的是

当你使用placement new来构造函数而不是预先分配的

缓冲区。 placement new可以被视为对构造函数的显式调用

,因为它实际上就是这样。


Calling the destructor twice is undefined behavior. The only place
where explicitly calling the destructor is required and justified is
when you used placement new to do the constructor over a pre-allocated
buffer. The placement new can be considered as being an explicit call
to the constructor as that is what it actually does.



对不起,我没有得到你的意见,你举个例子吗?

I''m sorry i didn''t get ur point, could u give an example?


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

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