"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