RAII

Discussion in 'C++' started by Naren, Oct 5, 2007.

  1. Naren

    Naren Guest

    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.
    Naren, Oct 5, 2007
    #1
    1. Advertising

  2. 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
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 5, 2007
    #2
    1. Advertising

  3. Naren

    Naren Guest

    "Erik Wikström" <> wrote in message
    news:jjsNi.10477$...
    > 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.
    Naren, Oct 5, 2007
    #3
  4. Naren

    Markus Moll Guest

    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
    Markus Moll, Oct 5, 2007
    #4
  5. Naren

    terminator Guest

    On Oct 5, 5:51 pm, "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.
    >
    > 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.
    terminator, Oct 5, 2007
    #5
  6. Naren

    Daniel Kraft Guest

    Naren wrote:
    > "Erik Wikström" <> wrote in message
    > news:jjsNi.10477$...
    >> 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!
    Daniel Kraft, Oct 5, 2007
    #6
  7. 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
    R Samuel Klatchko, Oct 5, 2007
    #7
  8. Naren

    Rolf Magnus Guest

    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.
    Rolf Magnus, Oct 7, 2007
    #8
  9. Naren

    terminator Guest

    On Oct 5, 8:56 pm, R Samuel Klatchko <> 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.
    terminator, Oct 9, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MikeB
    Replies:
    4
    Views:
    340
    Owen Jacobson
    Oct 26, 2004
  2. Markus Elfring

    RAII for value objects

    Markus Elfring, Nov 10, 2004, in forum: C++
    Replies:
    23
    Views:
    812
    Markus Elfring
    Jan 27, 2005
  3. Johannes Schaub (litb)

    Re: Why is RAII called RAII?

    Johannes Schaub (litb), Sep 12, 2010, in forum: C++
    Replies:
    2
    Views:
    389
    James Kanze
    Sep 18, 2010
  4. cpp4ever

    Re: Why is RAII called RAII?

    cpp4ever, Sep 12, 2010, in forum: C++
    Replies:
    1
    Views:
    397
    BGB / cr88192
    Sep 13, 2010
  5. Goran Pusic

    Re: Why is RAII called RAII?

    Goran Pusic, Sep 13, 2010, in forum: C++
    Replies:
    11
    Views:
    539
    ptyxs
    Sep 16, 2010
Loading...

Share This Page