Home » Coding » c++ » RAII
RAII [message #17263] Fri, 05 October 2007 10:51 Go to next message
Naren  is currently offline Naren
Messages: 2
Registered: October 2007
Junior Member
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.
Re: RAII [message #17265 is a reply to message #17263 ] Fri, 05 October 2007 10:59 Go to previous messageGo to next message
Erik-wikstrom  is currently offline Erik-wikstrom
Messages: 412
Registered: July 2007
Senior Member
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
Re: RAII [message #17266 is a reply to message #17265 ] Fri, 05 October 2007 11:01 Go to previous messageGo to next message
Naren  is currently offline Naren
Messages: 2
Registered: October 2007
Junior Member
"Erik Wikström" <Erik-wikstrom@telia.com> wrote in message
news:jjsNi.10477$ZA.7039@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.
Re: RAII [message #17267 is a reply to message #17263 ] Fri, 05 October 2007 11:02 Go to previous messageGo to next message
Markus Moll  is currently offline Markus Moll
Messages: 10
Registered: October 2007
Junior Member
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
Re: RAII [message #17269 is a reply to message #17266 ] Fri, 05 October 2007 13:21 Go to previous messageGo to next message
Daniel Kraft  is currently offline Daniel Kraft
Messages: 21
Registered: August 2007
Junior Member
Naren wrote:
> "Erik Wikström" <Erik-wikstrom@telia.com> wrote in message
> news:jjsNi.10477$ZA.7039@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!
Re: RAII [message #17272 is a reply to message #17263 ] Fri, 05 October 2007 11:45 Go to previous messageGo to next message
terminator  is currently offline terminator
Messages: 54
Registered: July 2007
Member
On Oct 5, 5:51 pm, "Naren" <a...@a.com> 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.
>
> 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.
Re: RAII [message #17289 is a reply to message #17272 ] Fri, 05 October 2007 13:56 Go to previous messageGo to next message
R Samuel Klatchko  is currently offline R Samuel Klatchko
Messages: 4
Registered: September 2007
Junior Member
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
Re: RAII [message #17390 is a reply to message #17289 ] Sun, 07 October 2007 07:55 Go to previous message
Rolf Magnus  is currently offline Rolf Magnus
Messages: 27
Registered: August 2007
Junior Member
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.
Previous Topic:Check If Object Deleted
Next Topic:looped include
Goto Forum:
  


Current Time: Fri May 16 02:27:31 EDT 2008

Total time taken to generate the page: 0.11442 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software