The destruction of a sub instantiated obj that I’mpointing too.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank

    The destruction of a sub instantiated obj that I’mpointing too.

    The destruction of a sub instantiated obj that I’m pointing too.

    This is a pseudo code example, don’t get all bent if I forgot a
    semicolon etc for something like some of you do. This will probably not
    compile, just an example to explain.

    Problem, how do I properly destruct the object that msg is pointing to
    in main? O I have to figure out what type of msg it is pointing to and
    explicitly call that destructor?

    class MsgCreator
    {
    Msg *MsgCreator::Cr eateMsg(int msgtype)
    {
    if (msgtype ==1)
    return new msg1();
    else
    return new msg2();
    }

    class Msg
    {

    }
    class msg1 : public msg
    {
    }
    class msg2:public msg
    {
    }

    main()
    {
    Msg *msg;
    MsgCreator *m = new MsgCreator();
    msg = m-> CreateMsg(1);

    delete msg;//does not seem to work, did not call the destructor
    }

    Thanks,

    Frank

  • Victor Bazarov

    #2
    Re: The destruction of a sub instantiated obj that I'm pointing too.

    "Frank" <fgeck@optonlin e.net> wrote...[color=blue]
    > The destruction of a sub instantiated obj that I'm pointing too.
    >
    > This is a pseudo code example, don't get all bent if I forgot a
    > semicolon etc for something like some of you do. This will probably not
    > compile, just an example to explain.[/color]

    If you get into a habit of writing correct code, you will have
    much less trouble with the language. Trust me. When asking
    questions about some code not working, what good does it do to
    post pseudo-code? Don't get all bent if I correct your code
    by mentioning a semicolon or a curly brace.
    [color=blue]
    > Problem, how do I properly destruct the object that msg is pointing to
    > in main? O I have to figure out what type of msg it is pointing to and
    > explicitly call that destructor?
    >
    > class MsgCreator
    > {[/color]

    public:
    [color=blue]
    > Msg *MsgCreator::Cr eateMsg(int msgtype)
    > {
    > if (msgtype ==1)
    > return new msg1();
    > else
    > return new msg2();
    > }[/color]
    };
    [color=blue]
    >
    > class Msg
    > {
    >[/color]

    You _have_ to add this:

    public:
    virtual ~Msg() {}

    even if 'Msg' has nothing to destroy. It is required by the
    language to have a virtual destructor if you intend on using
    'delete' to dispose of an object of a derived class through
    a pointer to the base class.
    [color=blue]
    > }[/color]
    ;
    [color=blue]
    > class msg1 : public msg
    > {
    > }[/color]
    ;
    [color=blue]
    > class msg2:public msg
    > {
    > }[/color]
    ;
    [color=blue]
    >
    > main()[/color]

    int main()
    [color=blue]
    > {
    > Msg *msg;
    > MsgCreator *m = new MsgCreator();
    > msg = m-> CreateMsg(1);
    >
    > delete msg;//does not seem to work, did not call the destructor[/color]

    What destructor? I could not see any destructor in the code
    you so kindly posted for our viewing pleasure.
    [color=blue]
    > }[/color]

    Read FAQ 5.8. It's very informative and if you follow its
    recommendations , your experience with the community here in
    comp.lang.c++ will be even more pleasant. You can find FAQ
    list here: http://www.parashift.com/c++-faq-lite/

    Victor


    Comment

    • Rolf Magnus

      #3
      Re: The destruction of a sub instantiated obj that I’m pointing too.

      Frank wrote:
      [color=blue]
      > The destruction of a sub instantiated obj that I’m pointing too.[/color]

      What?
      [color=blue]
      > This is a pseudo code example, don’t get all bent if I forgot a
      > semicolon etc for something like some of you do. This will probably
      > not compile, just an example to explain.[/color]

      Maybe others would like to try your example to find out what's wrong.
      Also, such errors distract from the real error, so please always post
      code that should work except from the error you observed.
      [color=blue]
      > Problem, how do I properly destruct the object that msg is pointing to
      > in main? O I have to figure out what type of msg it is pointing to
      > and explicitly call that destructor?[/color]

      Read up on polymorphism and virtual destructors in your favorite C++
      book. This is fundamental basics of C++ programming, so if you don't
      have a good C++ book, please get yourself one. It will save you a lot
      of learning time, believe me.
      [color=blue]
      > class MsgCreator
      > {
      > Msg *MsgCreator::Cr eateMsg(int msgtype)
      > {
      > if (msgtype ==1)
      > return new msg1();
      > else
      > return new msg2();
      > }
      >
      > class Msg
      > {
      >
      > }
      > class msg1 : public msg
      > {
      > }
      > class msg2:public msg
      > {
      > }
      >
      > main()
      > {
      > Msg *msg;
      > MsgCreator *m = new MsgCreator();
      > msg = m-> CreateMsg(1);
      >
      > delete msg;//does not seem to work, did not call the destructor
      > }[/color]

      Comment

      Working...