473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

partially initialized constructor


Hello,

I would like fooBar to be initialized to zero in the following scenario
where there is not enough memory to complete construction after
an object of sizeof(FooBar) has been allocated on the heap. Is
the standard way to deal with this situation in plain old standard
C++ to use a try catch block in main() and a throw inside the
constructor after cleaning up or can anyone suggest alternatives?

Thanks,

JG

class FooBar {
public:
FooBar();
private:
Foo *foo;
Bar *bar;
}

FooBar::FooBar() {
foo = new Foo(); // successful
bar = new Bar(); // unsuccessful
}

int main() {
FooBar *fooBar = new FooBar();
//...
}

Dec 3 '06 #1
5 1487

John Goche skrev:
Hello,

I would like fooBar to be initialized to zero in the following scenario
where there is not enough memory to complete construction after
an object of sizeof(FooBar) has been allocated on the heap. Is
the standard way to deal with this situation in plain old standard
C++ to use a try catch block in main() and a throw inside the
constructor after cleaning up or can anyone suggest alternatives?
The first thing to do is ask yourself why you use raw pointers. You
should realise that as soon as you unconditionally delete the pointers
in the destructor is that the (implied) copy constructor and assignment
operator suddenly does not work. Assuming you disable these, one
solution could be std::auto_ptr:
>
Thanks,

JG

class FooBar {
public:
FooBar();
private:
Foo *foo;
Bar *bar;
}

FooBar::FooBar() {
foo = new Foo(); // successful
bar = new Bar(); // unsuccessful
std::auto_ptr<Fooautofoo(new Foo());
std::auto_ptr<Barautobar(new Bar());
foo = autofoo.reset();
bar = autobar.reset();
}
>
int main() {
FooBar *fooBar = new FooBar();
No reason to use new here. Watch out for the Java syndrome! Perhaps
pointers aren't really appropriate for your solution and again - if yo
need a pointer in your class, quite probably it should not be raw.

/Peter

Dec 3 '06 #2
John Goche wrote:
>
Hello,

I would like fooBar to be initialized to zero in the following scenario
where there is not enough memory to complete construction after
an object of sizeof(FooBar) has been allocated on the heap. Is
the standard way to deal with this situation in plain old standard
C++ to use a try catch block in main() and a throw inside the
constructor after cleaning up or can anyone suggest alternatives?
Yes. You want to throw in the constructor if construction does not succeed.
However, cleanup can be handled without try-throw-catch.
class FooBar {
public:
FooBar();
private:
Foo *foo;
Bar *bar;
}

FooBar::FooBar() {
foo = new Foo(); // successful
bar = new Bar(); // unsuccessful
}

int main() {
FooBar *fooBar = new FooBar();
//...
}
A standard idiom, is to use auto_ptr<>:

#include <memory>

typedef int Foo;
typedef int Bar;

class FooBar {
public:
FooBar();
private:
Foo *foo;
Bar *bar;
};

FooBar::FooBar() {
std::auto_ptr<Foofoo_a ( new Foo() );
std::auto_ptr<Barbar_a ( new Bar() );
foo = foo_a.release();
bar = bar_a.release();
}

int main() {
try {
FooBar *fooBar = new FooBar();
// ...
}
catch ( whatever_may_be_thrown ) {
}
}

This generalized easily to an arbitrary number of pointer members.

Best

Kai-Uwe Bux
Dec 3 '06 #3

peter koch skrev:
[snip]
foo = autofoo.reset();
bar = autobar.reset();
Oops - should be release - not reset, of course.

/Peter

Dec 3 '06 #4
I would like fooBar to be initialized to zero in the following scenario
where there is not enough memory to complete construction after
an object of sizeof(FooBar) has been allocated on the heap.
I don't think you can force the new operator to return 0 based on logic
inside your constructor. However you can force new to return 0 if the
actual allocation of FooBar fails:

FooBar *fooBar = new(std::nothrow) FooBar();
Is the standard way to deal with this situation in plain old standard
C++ to use a try catch block in main() and a throw inside the
constructor after cleaning up or can anyone suggest alternatives?
Here is another common way, without using auto_ptr as other posters
have shown

FooBar::FooBar()
{
Foo* f = 0;
Bar* b = 0;
try
{
f = new Foo();
b = new Bar();
}
catch(std::bad_alloc b)
{
// rethrow the exception
throw;
}

foo = f;
bar = b;
}

-
Ivan
http://www.0x4849.net

Dec 4 '06 #5
"Ivan Novick" <iv**@0x4849.netwrites:
>Is the standard way to deal with this situation in plain old standard
C++ to use a try catch block in main() and a throw inside the
constructor after cleaning up or can anyone suggest alternatives?
Here is another common way, without using auto_ptr as other posters
have shown

FooBar::FooBar()
{
Foo* f = 0;
Bar* b = 0;
try
{
f = new Foo();
b = new Bar();
}
catch(std::bad_alloc b)
{
You need to 'delete f;' here.
// rethrow the exception
throw;
}

foo = f;
bar = b;
}
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Dec 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marc W | last post by:
Hello, I am trying to write some code, I get a compiler error, but the help about it is very limited. (I am working with MSVC++ 6.0.) Could you give me a hint what is wrong here please? ...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
1
by: Martin Magnusson | last post by:
I have a partially specialized templated class, which at one point needs to pass pointers to some of its methods to another function. However, it seems that my compiler (gcc) and me don't quite...
3
by: Ryan Steckler | last post by:
I found this behavior while trying to implement a singleton class. Below is a somewhat more straight forward example, though admittedly less useful in real life. The basic problem is that when a...
2
by: Javaman59 | last post by:
I want to add a ValueChanged event handler to a control. If I add the handler during form construction then the ValueChanged handler gets called when the control is initialized, and I don't want...
2
by: TEK | last post by:
Hello We're having a issue with a part of our app that is very strange, and I'm unable to see that it should be possible. We have: A collection (ActivityCollection) that is inherited from a...
3
by: ww | last post by:
I read a couple of books that tells this, but seems not one can explain why is this thanks
2
by: Olumide | last post by:
Hello C++ gurus, I've got this basic class thats the backbone of my project and I frequently use use an instance of it to initialize other objects like so. However, my latest class, a simple...
10
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.