473,508 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 1404
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1847
by: Gandalf | last post by:
Hello (it's the newbie again). If I have a class class Foo{ public: Foo(){cout<<"Making"<<endl;} ~Foo(){cout<<"Destroying"<<endl;} }; void func(Foo x){}
0
1226
by: Stephan Keil | last post by:
Hi all, consider a class class X { static X& OneX() { static X sgl1; return sgl1; } static X& TheX() { static X sgl2(OneX()); return sgl2; } }; and two source files with global variables
8
21708
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
2
1265
by: Slawomir Lisznianski | last post by:
Do ref classes indeed support instantiation on the stack? Similarly, can native classes be instantiated on gc heap? When you try to have a ref class instance on the stack, you'll get: R :...
9
2990
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
6
2232
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
5
1432
by: Parapura Rajkumar | last post by:
hey all class A { }; class B {
2
4301
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
3
1307
by: deciacco | last post by:
I'm using an object that has a dataset within it. When the object is instantiated I populate the dataset from xml files. I want the object to save the dataset back to the xml file before it is...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project用lanning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.