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

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default 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::CreateMsg(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
    }



    Frank Guest

  2. Similar Questions and Discussions

    1. Bug when events are instantiated by AS3 instead of MXML
      In example 1 the events rollOver and rollOut are initialized using MXML. When you run the example you can see when you rollOver the Canvas the...
    2. Finding out if web service instantiated object
      I have a vb class that manages all of my session and query stirng parameters plus the user login. I would like to use it within my web web service...
    3. [PHP-DEV] [PATCH] Two bugs in variable destruction
      Hello. There are two bugs that show when code is executed from an object destructor and PHP is compiled in debug mode. Both bugs cause the...
    4. Sudden self-destruction of SuSE 8.2 installation?
      Hi, I didn't see it coming, out of the blue my SuSE 8.2 installation appears to be completely ruined. What I did when it happened is to mount a...
    5. Height function and instantiated objects
      Hi I was just wondering if Flash MX has a patch on instantiated objects and the hieght function.Ive been sought of recreating the wheel with some...
  3. #2

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

    On Thu, 07 Aug 2003 17:26:13 GMT, Frank <fgeck@optonline.net> wrote:
    > The destruction of a sub instantiated obj that I’m pointing too.
    >
    [snip]

    Response:

    1. This belongs in a C++ newsgroup, not here.
    2. Hard to read the psuedo code, but I think the problem is that you should
    make the destructors virtual. Look this up in a good C++ book and it will
    explain why that's important.

    --Marc
    Marc Rochkind Guest

  4. #3

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

    Frank <fgeck@optonline.net> writes:
    > 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::CreateMsg(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
    I don't want to appear to be getting all bent, but there aren't any
    destructors in what you show, so it's hard to say what the problem
    is. Also, Marc is correct in suggesting that this would be better
    asked on a C++ newsgroup.

    I suspect he's also right about needing a virtual destructor, but it's
    hard to tell from what's given above. It wouldn't have been that much
    additional effort to post something that actually compiles and
    runs. This works as expected with g++

    #include <iostream>

    class Msg
    {
    public:
    virtual ~Msg() {cout << "Msg::~Msg()\n";}
    };

    class msg1 : public Msg
    {
    public:
    ~msg1() {cout << "msg1::~msg1()\n";}
    };

    class msg2:public Msg
    {
    public:
    ~msg2() {cout << "msg2::~msg2()\n";}
    };

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

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

    Joe
    Joe Halpin Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139