By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

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

P: n/a
The destruction of a sub instantiated obj that I知 pointing too.

This is a pseudo code example, don稚 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
}

Thanks,

Frank

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Frank" <fg***@optonline.net> wrote...
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.
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.
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
{
public:
Msg *MsgCreator::CreateMsg(int msgtype)
{
if (msgtype ==1)
return new msg1();
else
return new msg2();
} };

class Msg
{

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.
} ;
class msg1 : public msg
{
} ;
class msg2:public msg
{
} ;

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

delete msg;//does not seem to work, did not call the destructor
What destructor? I could not see any destructor in the code
you so kindly posted for our viewing pleasure.
}


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
Jul 19 '05 #2

P: n/a
Frank wrote:
The destruction of a sub instantiated obj that I知 pointing too.
What?
This is a pseudo code example, don稚 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.
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.
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?
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.
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
}


Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.