例外&构造函数 [英] Exceptions & Constructors

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

问题描述

MyClass中的构造函数通过

''new'实例化很多对象指针,我想实现一种方法来确保已经分配了对象

在内存中通过catch(ing)bad_alloc异常

错误,但这意味着我必须从

MyClass构造函数中抛出此错误,我该如何避免这种情况? br />

谢谢大家!

The constructor in MyClass instantiates many objects pointers through
''new'', I would like to implement a way to make sure that the object
has been allocated in memory by catch(ing) the bad_alloc exception
error, but this means that I have to throw this error back from
MyClass constructor, how can I avoid this?

Thanks folks!

推荐答案

2007-07-30 13:15,< a href =mailto:ja ****** @ gmail.com> ja ****** @ gmail.com 写道:
On 2007-07-30 13:15, ja******@gmail.com wrote:

MyClass中的构造函数实例化了很多对象指针,通过

''new'',我想实现一种方法来确保对象

已经分配了内存通过catch(ing)bad_alloc异常

错误,但这意味着我必须从

MyClass构造函数中抛出此错误,我该如何避免这种情况?
The constructor in MyClass instantiates many objects pointers through
''new'', I would like to implement a way to make sure that the object
has been allocated in memory by catch(ing) the bad_alloc exception
error, but this means that I have to throw this error back from
MyClass constructor, how can I avoid this?



当然,在类中添加一个标志,告诉对象是否构造了

正确:


class Foo {

int * ptrarr [16];

public:

bool正确;

Foo()正确(真实){

尝试{

for(size_t i = 0; i< 16; ++ i)

ptrarr [i] = new int();

} catch(bad_alloc&){

correct = false;

}

}

};


int main(){

Foo f;

if(f.correct == false){

// Opps,未能分配

}

}


-

Erik Wikstr?m

Sure, add a flag to the class that tells if the object was constructed
correctly:

class Foo {
int* ptrarr[16];
public:
bool correct;
Foo() correct(true) {
try {
for (size_t i = 0; i < 16; ++i)
ptrarr[i] = new int();
} catch(bad_alloc&) {
correct = false;
}
}
};

int main() {
Foo f;
if (f.correct == false) {
// Opps, failed to allocate
}
}

--
Erik Wikstr?m


< ja ****** @ gmail.comwrote in message

新闻:11 ********************** @ l70g2000hse.googlegr oups.com ...
<ja******@gmail.comwrote in message
news:11**********************@l70g2000hse.googlegr oups.com...

MyClass中的构造函数通过

''new'实例化许多对象指针,我想实现一种方法来确保在对象

已经通过catch(ing)bad_alloc异常

错误在内存中分配,但这意味着我必须从
返回此错误
MyClass构造函数,我该如何避免这种情况?
The constructor in MyClass instantiates many objects pointers through
''new'', I would like to implement a way to make sure that the object
has been allocated in memory by catch(ing) the bad_alloc exception
error, but this means that I have to throw this error back from
MyClass constructor, how can I avoid this?



那么,你希望你的代码在失败时做什么?


考虑:

class Foo

{

// ...

};


int main()

{

Foo Bar;

}


你想要什么如果Bar因为在Foo的构造函数中分配不好的
而无法实例化?那你怎么想检查它是否成功了?


我能想到的一些事情就是我的头脑。


1.让Foo包含一个私有bool变量,说明所有指针是否已使用newed实例正确实例化
。替代方法是,如果变量的初始化失败,则检查任何指针本身是否为NULL值,您将在

构造函数中设置该值。 Foo可能有一个

方法返回,如果它是initalized或不。 Foo可以用它的内部

方法检查初始化。


2.让Foo只由工厂创建(I.E返回一个指针)。如果Foo可以

没有被实例化,工厂将返回NULL,主线可以检查。


3.什么都不做,并且新的'throw'传播到主线


我可以想到一些其他的方法,取决于你想要做什么。

让一个实例漂浮在那里是不是一件好事没有

正确实例化。如果你不允许新的'投掷到传播',你将如何证明这一点?你想怎么防止呢?

Well, what do you want your code to do on failure?

Consider:

class Foo
{
// ...
};

int main()
{
Foo Bar;
}

What do you want to happen if Bar can not be instantized because of bad
allocation inside of Foo''s constructor? And how do you want to check if it
was successful?

Some things I can think of off the top of my head.

1. Have Foo contain a private bool variable stating if all pointers have
been instantized correctly with newed instances. Alternative to this, is to
check any of the pointers itself for a NULL value which you would set in the
constructor if initializaiton of the variables failed. Foo could have a
method returning if it was initalized or not. Foo could have it''s internal
methods checking initialzation.

2. Have Foo created only by a factory (I.E returning a pointer). If Foo can
not be instantized, the factory returns NULL which mainline can check.

3. Do nothing and have new''s throw propogate up to mainline

I could think of a few other methods depending on what you would want done.
It is not a good thing to have an instance floating around that was not
properly instantized. How are you going to provent this if you don''t allow
new''s throw to propogate? How do you WANT to prevent it?


Erik Wikstr?m写道:
Erik Wikstr?m wrote:

2007-07- 30 13:15, ja******@gmail.com 写道:

> MyClass中的构造函数通过
new实例化许多对象指针,我想实现一种方法来确保对象
已经分配了内存通过catch(ing)bad_alloc异常
错误,但这意味着我必须从
MyClass构造函数中抛出此错误,我该如何避免这种情况?
>The constructor in MyClass instantiates many objects pointers through
''new'', I would like to implement a way to make sure that the object
has been allocated in memory by catch(ing) the bad_alloc exception
error, but this means that I have to throw this error back from
MyClass constructor, how can I avoid this?



当然,在类中添加一个标志,告诉对象是否构造了

正确:


class Foo {

int * ptrarr [16];

public:

bool正确;

Foo()正确(真实){

尝试{

for(size_t i = 0; i< 16; ++ i)

ptrarr [i] = new int();

} catch(bad_alloc&){

correct = false;

}

}

};


int main(){

Foo f;

if(f.correct == false){

// Opps,未能分配

}

}


Sure, add a flag to the class that tells if the object was constructed
correctly:

class Foo {
int* ptrarr[16];
public:
bool correct;
Foo() correct(true) {
try {
for (size_t i = 0; i < 16; ++i)
ptrarr[i] = new int();
} catch(bad_alloc&) {
correct = false;
}
}
};

int main() {
Foo f;
if (f.correct == false) {
// Opps, failed to allocate
}
}



如果代码失败,代码就会泄漏。您是否不必删除所有这些

或者是否分配空指针?

That code leaks if it fails. Should you not have to delete all of them
or at lease assign a null pointer ?


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

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