473,394 Members | 1,735 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,394 software developers and data experts.

RAII

Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.

Application created using Microsoft compiler.

Thanks in advance.

Thanks,
Naren.
Oct 5 '07 #1
8 1481
On 2007-10-05 16:51, Naren wrote:
Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.
In what way? I would probably use the first one, but it depends on what
A and B are and how I would use them.

--
Erik Wikström
Oct 5 '07 #2

"Erik Wikström" <Er***********@telia.comwrote in message
news:jj*****************@newsb.telia.net...
On 2007-10-05 16:51, Naren wrote:
>Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.

In what way? I would probably use the first one, but it depends on what
A and B are and how I would use them.
Since I am in the destructor, any issues?

Thanks for the response.

Thanks,
Naren.
Oct 5 '07 #3
Hi

Naren wrote:
Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.
No, but the first one will (probably... see further below). The second one
suffers from both a missing copy-constructor and a missing assignment
operator. Try (with the second one)

int main()
{
B b1;
B b2;
B b3 = b1;
b2 = b3;
}

This is very likely to crash. I also don't see why you call the
functions "Doif" and "Doelse", because I fail to see any "decision" here.
Is first one undefined, though I have verified that both works correctly.
Um... If you have verified that both work correctly, then why do you ask?
No, the first one should be fine and better than the second. However, of
course it also depends on A, so without knowing what A does, it's hard to
judge if even your first attempt is correct.
Application created using Microsoft compiler.
That shouldn't matter.

Markus

Oct 5 '07 #4
Naren wrote:
"Erik Wikström" <Er***********@telia.comwrote in message
news:jj*****************@newsb.telia.net...
>On 2007-10-05 16:51, Naren wrote:
>>Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.
In what way? I would probably use the first one, but it depends on what
A and B are and how I would use them.

Since I am in the destructor, any issues?
No, as in the second version, a is destructed after the destructor of B
is finished, so in it you can work with a to your liking.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Oct 5 '07 #5
On Oct 5, 5:51 pm, "Naren" <a...@a.comwrote:
Please tell me which of this is correct.

class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}

};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}

};

Second one will always work.

Is first one undefined, though I have verified that both works correctly.

Application created using Microsoft compiler.

Thanks in advance.

Thanks,
Naren.
first one looks more efficient and likely to work,the second deals
with dynamic allocation which is far more complex to handle.

regards,
FM.

Oct 5 '07 #6
terminator wrote:
>class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}

};

class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}

};
>
first one looks more efficient and likely to work,the second deals
with dynamic allocation which is far more complex to handle.
Dynamic allocation is more complex to handle because it gives you the
ability to detect low memory conditions. When the heap is full (or too
fragmented) to fulfill your request, new will throw and you will
probably want to handle that situation.

But if the stack is full, when you try to put another object on the
stack, the behavior is undefined.

samuel
Oct 5 '07 #7
R Samuel Klatchko wrote:

Dynamic allocation is more complex to handle because it gives you the
ability to detect low memory conditions. When the heap is full (or too
fragmented) to fulfill your request, new will throw and you will
probably want to handle that situation.
Depends. A typical desktop system will become unresponsive long before that.

Oct 7 '07 #8
On Oct 5, 8:56 pm, R Samuel Klatchko <r...@moocat.orgwrote:
terminator wrote:
class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};
class B
{
A* a;
public:
B():a(new A) {a->Doif();}
~B(){a->Doelse();delete a;}
};
first one looks more efficient and likely to work,the second deals
with dynamic allocation which is far more complex to handle.

Dynamic allocation is more complex to handle because it gives you the
ability to detect low memory conditions. When the heap is full (or too
fragmented) to fulfill your request, new will throw and you will
probably want to handle that situation.

But if the stack is full, when you try to put another object on the
stack, the behavior is undefined.
but you have to take the risk of ill-programming the
[de]allocation,and as mentioned above (by the experts) you will need
to define the assignment and copy-ctor in addition to default-ctor and
dtor.
on the other hand large objects should be kept of stack and one can
use some smart pointer(eg. boost::shared_ptr or std::auto_ptr) to an
object instead of encapsulating a pointer inside an object:

class B;//a huge data type
class B
{
A a;
public:
B(){a.Doif();}
~B(){a.Doelse();}
};
std::auto_ptr<Bbptr (new B);
bptr->DoSomthing();
DoSomthingWith(*bptr);
regards,
FM.

Oct 9 '07 #9

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

Similar topics

4
by: Pierre Rouleau | last post by:
As much as I love Python, I sometimes find myself wishing Python supported the RAII idiom (resource acquisition is initialization) that is available in C++, the emerging D language, and others. ...
26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
4
by: MikeB | last post by:
Hi, Recently I was asked to look at some code where RAII is used to ensure automatic cleanup of a resource. Unfortunately, cleaning up the resource requires that the destructor make a call...
23
by: Markus Elfring | last post by:
The class "auto_ptr" implements the RAII pattern for pointer types. It seems that an implementation is not provided for non-pointer values by the STL so far. I imagine to use the "acquisition" for...
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
6
by: tomthemighty | last post by:
A simple RAII wrapper for acquiring a Thingy that has two alternative acquire methods, acquire() and acquireAndFurtle(): class ThingyAcquirer : private boost::noncopyable { public: explicit...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.