Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > RAII

Reply
 
 
Naren
Guest
Posts: n/a
 
      10-05-2007
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.


 
Reply With Quote
 
 
 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      10-05-2007
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
 
Reply With Quote
 
 
 
 
Naren
Guest
Posts: n/a
 
      10-05-2007

"Erik Wikström" <(E-Mail Removed)> wrote in message
news:jjsNi.10477$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      10-05-2007
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

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      10-05-2007
On Oct 5, 5:51 pm, "Naren" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Daniel Kraft
Guest
Posts: n/a
 
      10-05-2007
Naren wrote:
> "Erik Wikström" <(E-Mail Removed)> wrote in message
> news:jjsNi.10477$(E-Mail Removed)...
>> 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!
 
Reply With Quote
 
R Samuel Klatchko
Guest
Posts: n/a
 
      10-05-2007
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-07-2007
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.

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      10-09-2007
On Oct 5, 8:56 pm, R Samuel Klatchko <(E-Mail Removed)> wrote:
> 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<B> bptr (new B);
bptr->DoSomthing();
DoSomthingWith(*bptr);


regards,
FM.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why is RAII called RAII? Johannes Schaub (litb) C++ 2 09-18-2010 09:18 AM
Re: Why is RAII called RAII? Goran Pusic C++ 11 09-16-2010 03:11 PM
Re: Why is RAII called RAII? cpp4ever C++ 1 09-13-2010 06:25 AM
RAII for value objects Markus Elfring C++ 23 01-27-2005 08:41 PM
RAII / handling failures during destruction - advice required MikeB C++ 4 10-26-2004 05:22 AM



Advertisments