关于构造函数的一些基本问题&异常(在托管C ++环境中) [英] A few basic questions about constructor & exception (in Managed C++ environment)

查看:58
本文介绍了关于构造函数的一些基本问题&异常(在托管C ++环境中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(不是我使用2.0,所以新的返回一个正常指针和gcnew返回一个托管的

一个,我的问题下面关于新关注的简单标准C ++分配器)


- 如果我使用默认的new运算符,是所有实例变量

初始化为NULL / 0?

- 如果内存不足则发生新的?是否返回NULL

或抛出异常?

- 如果new抛出本机C ++异常,托管C ++会发生什么?!

- 如果在construtor中有一个例外,是〜MyClass()调用了吗?

- 如果我想在构造函数中抛出(托管)异常我应该免费

所有本机

类型之前?或者我应该抛出并让析构函数(在删除之前检查无效

)完成它的工作?


-

那里这个世界上有10种人。那些懂二进制的人和那些不知道的人。

解决方案

所以我把你的问题与原生C ++相关,除了最后一个。

- 如果我使用默认的new运算符,所有实例变量
初始化为NULL / 0?


默认情况下没有任何内容被初始化。就像malloc一样,内存包含

随机内容。除非new创建一个对象,其构造函数

初始化其成员。

- 如果没有足够的内存,那么新的内容会发生什么?它会返回NULL
或抛出异常吗?


抛出std :: bad_alloc。

- 如果new抛出本机C ++异常,托管C ++会发生什么?


C ++异常不能移植到其他编译器(在同一供应商的不同版本之间通常甚至不是
)。而且,它们不是在不同语言之间移植的。我会说它会导致

崩溃。您永远不应该调用从
托管代码抛出的非托管函数,始终确保捕获所有异常并使用返回值

值:


bool UnmanagedFunction()

{

试试

{

[...]

}

catch(...)

{

返回false;

}

}


您必须对C和COM包装器执行相同的操作。

- 如果在construtor中有例外,是〜MyClass()调用?


编号如果构造函数中有异常,则该对象不是完全构造的
,因此析构函数不能

跟注。然而,已经构建的构件的析构函数已经被构造了。


所以除非你使用智能指针,否则你不应该抛弃首先解除分配之前的

构造函数。在扔之前你还应该取消初始化

所有东西。尝试养成使用智能

指针和其他RAII技术的习惯。例如,当你打开一个文件时,

写一个包装类,其destrucor执行逆操作

(关闭文件)。这样你就更安全了。


你永远不应该允许析构函数抛出。在大多数情况下,这可能导致

立即崩溃。您的应用程序可能会立即从任务栏中消失而没有任何警告消息。

- 如果我想在构造函数中抛出(托管)异常我应该免费
所有原生的
类型之前?或者我应该扔掉并让析构函数(在删除之前检查无效)完成它的工作?




我不知道。在.NET中没有desturctor这样的东西,只有

C ++ / CLI语言支持它,它仍处于草案阶段。您可以通过编写测试应用程序轻松确认它,这就是我将如何尝试

它。您可以尝试在草案标准中找到它。也许别人可以为你回答这个问题。一本好的C ++ / CLI书肯定会是非常有用的* * * *


Tom


< blockquote>谢谢,非常完整。

和恼人的..我的意思是std :: bad_alloc,如果不够的话

memopry是一个哈塞尔......嗯。 ..我必须考虑一下。

谢谢!


-

这个世界上有10种人。那些懂二元的人和那些不知道的人。

Tamas Demjen < TD ***** @ yahoo.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...

所以我把你的问题与原生C ++相关,除了最后一个。

- 如果我使用默认的new运算符,所有实例变量
初始化为NULL / 0?



默认情况下没有任何内容被初始化。就像malloc一样,内存包含随机内容。除非new创建一个对象,其构造函数初始化其成员。

- 如果没有足够的内存,新的内容会发生什么?它会返回
NULL
或抛出异常吗?



抛出std :: bad_alloc。

- 如果是新的抛出托管C ++中发生的本机C ++异常?!



C ++异常不能移植到其他编译器(通常甚至不在同一供应商的不同版本之间)。而且,它们不能在不同语言之间移植。我会说它会导致崩溃。您应该永远不会调用托管代码抛出的非托管函数,始终确保捕获所有异常并使用返回值:

bool UnmanagedFunction()
{
尝试
{
[...]
}
赶上(...)
{
返回false;

- 如果在construtor中有例外,是~MyClass()调用?



如果构造函数中有异常,则该对象不被认为是完全构造的,因此析构函数可以''
被叫。然而,已经构建其成员的析构函数被称为。

因此,除非您使用智能指针,否则在释放之前不应该从
构造函数中抛出第一。在扔之前你也应该取消初始化一切。尝试养成使用智能指针和其他RAII技术的习惯。例如,当您打开文件时,
编写一个包装类,其destrucor执行逆操作
(关闭文件)。这样你就更安全了。

你永远不应该允许析构函数抛出。在大多数情况下,这可能导致直接崩溃。您的应用程序可能会立即从任务栏中消失,而不会发出任何警告消息。

- 如果我想在构造函数中抛出(托管)异常,我应该免费使用
所有原生的
类型之前?或者我应该抛出并让析构函数(在删除之前检查
nullity
)完成它的工作?



我不知道。在.NET中没有desturctor这样的东西,只有支持它的C ++ / CLI语言,它仍处于草案阶段。您可以通过编写测试应用程序轻松确认它,这就是我将如何尝试它。您可以尝试在草案标准中找到它。也许其他人可以为你回答这个问题。一本好的C ++ / CLI书肯定非常有用*视觉*。

Tom



std: :bad_alloc很好,而不是hassel。但是,如果你不喜欢

例外,只需使用nothrow即可。这样的版本:


int * i = new(std :: nothrow)int [50];


请务必包含< ; new>


(not I use 2.0, so new return a "normal" pointer and gcnew return a managed
one, my question below regarding new concern plain standart C++ allocator)

- if I use the default new operator, are all the instance variable
initialize to NULL / 0 ?
- if there is not enough memory what happend with new ? does it return NULL
or throw an exception?
- if new throw a native C++ exception what happen in Managed C++ ?!
- if there is an exception in a construtor, is ~MyClass() called?
- if I want to throw a (managed) exception in a constructor should I free
all native
type before? or should I throw and let the destructor (which check nullity
before deleting) do its job?

--
There are 10 kinds of people in this world. Those who understand binary and
those who don''t.

解决方案

So I take your questions are native C++ related, except the last one.

- if I use the default new operator, are all the instance variable
initialize to NULL / 0 ?
Nothing is initialized by default. Just like malloc, the memory contains
random content. Unless new creates an object, whose constructor
initializes its members.
- if there is not enough memory what happend with new ? does it return NULL
or throw an exception?
throw std::bad_alloc.
- if new throw a native C++ exception what happen in Managed C++ ?!
C++ exceptions are not portable to other compilers (often not even
between different versions of the same vendor). Also, they''re not
portable between different languages. I would say it''s going to cause a
crash. You should never call an unmanaged function that throws from
managed code, always make sure to catch all exceptions and use a return
value:

bool UnmanagedFunction()
{
try
{
[...]
}
catch(...)
{
return false;
}
}

You have to do the same for C and COM wrappers.
- if there is an exception in a construtor, is ~MyClass() called?
No. If there''s an exception in the constructor, the object is not
considered fully constructed, and therefore the destructor can''t be
called. However, the destructors for its members that have already been
constructed are called.

So unless you use a smart pointer, you shouldn''t throw from the
constructor before deallocating first. You should also uninitialize
everything before throwing. Try to pick up the habit of using smart
pointers and other RAII techniques. For example, when you open a file,
write a wrapper class whose destrucor performs the inverse operation
(closing the file). That way you''re much safer.

You should never allow the destructor to throw. That can cause an
immediate crash under most cases. Your application may immediately
disappear from the task bar without any warning message.
- if I want to throw a (managed) exception in a constructor should I free
all native
type before? or should I throw and let the destructor (which check nullity
before deleting) do its job?



I don''t know. There is no such thing as a desturctor in .NET, only the
C++/CLI language supports it, which is still in a draft stage. You can
easily confirm it by writing a test application, that''s how I would try
it. You can try to find it in the draft standard. Maybe someone else can
answer that question for you. A good C++/CLI book would certainly be
very very useful *sight*.

Tom


thanks, very complete.
and annoying as well.. I mean the std::bad_alloc in case of not enough
memopry is a hassel... mmh... I have to think on that.
thanks!

--
There are 10 kinds of people in this world. Those who understand binary and
those who don''t.
"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

So I take your questions are native C++ related, except the last one.

- if I use the default new operator, are all the instance variable
initialize to NULL / 0 ?



Nothing is initialized by default. Just like malloc, the memory contains
random content. Unless new creates an object, whose constructor
initializes its members.

- if there is not enough memory what happend with new ? does it return
NULL
or throw an exception?



throw std::bad_alloc.

- if new throw a native C++ exception what happen in Managed C++ ?!



C++ exceptions are not portable to other compilers (often not even between
different versions of the same vendor). Also, they''re not portable between
different languages. I would say it''s going to cause a crash. You should
never call an unmanaged function that throws from managed code, always
make sure to catch all exceptions and use a return value:

bool UnmanagedFunction()
{
try
{
[...]
}
catch(...)
{
return false;
}
}

You have to do the same for C and COM wrappers.

- if there is an exception in a construtor, is ~MyClass() called?



No. If there''s an exception in the constructor, the object is not
considered fully constructed, and therefore the destructor can''t be
called. However, the destructors for its members that have already been
constructed are called.

So unless you use a smart pointer, you shouldn''t throw from the
constructor before deallocating first. You should also uninitialize
everything before throwing. Try to pick up the habit of using smart
pointers and other RAII techniques. For example, when you open a file,
write a wrapper class whose destrucor performs the inverse operation
(closing the file). That way you''re much safer.

You should never allow the destructor to throw. That can cause an
immediate crash under most cases. Your application may immediately
disappear from the task bar without any warning message.

- if I want to throw a (managed) exception in a constructor should I free
all native
type before? or should I throw and let the destructor (which check
nullity
before deleting) do its job?



I don''t know. There is no such thing as a desturctor in .NET, only the
C++/CLI language supports it, which is still in a draft stage. You can
easily confirm it by writing a test application, that''s how I would try
it. You can try to find it in the draft standard. Maybe someone else can
answer that question for you. A good C++/CLI book would certainly be very
very useful *sight*.

Tom



std::bad_alloc is good, not a "hassel". However, if you don''t like
exceptions, just use the "nothrow" version like this:

int* i = new(std::nothrow) int[50];

Just be sure to include <new>


这篇关于关于构造函数的一些基本问题&amp;异常(在托管C ++环境中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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