Clearly you still don't understand or you are trying to be obtuse.
1) I stated that it is necessary to understand what is happening. If the
parent has state which it needs to clean up, then the destructor cannot be
virtual as the destructor of the parent class will not be called if
operating on a child class. I guess that you only use parent classes with
no state or you let them leak.
That comes under section (4) - you don't understand how virtual destructors
work. Perhaps you need remedial C++ training.
So here's some remedial C++ training for you.
class Object {
public:
virtual ~Object()
{
printf("Object Destructor called\n");
};
}
class Table : public Object
{
public:
virtual ~Table()
{
printf("Table Destructor called\n");
};
}
class IKEAGlassTop : public Table
{
char* pMemory;
public:
IKEAGlassTop()
{
pMemory = new char[1000];
}
virtual ~IKEAGlassTop()
{
printf("IKEAGlassTop Destructor called\n");
delete pMemory;
};
}
So I create a new IKEAGlassTop table, and pass it to something else which
understands Table objects. What happens when the destructor on my Table is
now called?
Answer:
If I have a virtual destructor, there is a vtable lookup, which locates the
IKEAGlassTop destructor, which is then called, deleting the memory
allocated when IKEAGlassTop was created. Then, the Tablet destructor is
called, followed by the Object destructor. You get the following output:
IKEAGlassTop Destructor called
Table Destructor called
Object Destructor called
Now... what happens if you do the same without a virtual destructor?
The Tablet destructor is called, which then calls the Object destructor.
The memory allocated in IKEAGlassTop's constructor is not free'd, and a
memory leak ensues.
THIS IS THE EXACT SCENARIO YOU CLAIM HAPPENS *IF YOU HAVE VIRTUAL
DESTRUCTORS*.
Your output on the screen will be this:
Table Destructor called
Object Destructor called
.... and if you don't believe me, then you might want to try running the
code for yourself.
Face it, Ian, you don't know half of what you'd like to think you know
about C++ programming.
2) If each class ensures that its requirements are met, there can never
be a case where the requirements are not met (robustness)
No. If you wanted robustness you'd wrap the startup/shutdown in a
singleton. You're doing a lot more work than you need to.
3) Once again you are being wantonly obtuse.
No, you keep mixing up Factory patterns claiming that they're only ever
used with State machines - you obviously have read through the GOF book
ONCE, and have definite misconceptions about what those patterns are.
Here's a refresher course for you:
http://www.codeproject.com/csharp/csdespat_1.asp
Read the Factory Pattern section. Do not claim I'm being obtuse until you
have read the Factory Pattern section. Do not claim I'm being obtuse until
you agree that Factory Pattern is a Creational pattern, not a Behavioral
pattern. Do not claim I'm being obtuse until you read the Design Patterns
book again, instead of arguing out of your ass.
4) Now repeat after me "If a parent class has its own objects that it
needs to clean up, the destructor on the parent cannot be virtual."
Appearantly you don't understand that. I feel sorry for your employer.
Ian, you're a ****ing moron who probably learned C++ from the back of a
crackerjack box, and I'd love to talk to your employer and let them know
what a worthless, egotistical dumbshit they have working for them.
Come back after you've run your own tests, and after you've read through
the C++ spec and seen how virtual destructors work.
YOU'VE GOT THEM ALL WRONG YOU IDIOT.
5) Allocation of memory is not the only problem. It takes a considerable
amount of time to create complex objects. Hence, it is important to
precreate complex objects. It is also good practice to precreate all the
objects that are required so that they do not need to be created on the
fly. I guess that you're just another Windows programmer who thinks that
being frugal with resources is not important. You can always throw a
bigger processor at the problem.
I guess you're yet another dickwad who doesn't know the first thing about
programming C++, and probably doesn't know much about speed vs. memory vs.
startup-time constraints when tuning code for performance.
How can you precreate "all objects that are required" if you don't know how
many there will be yet?
I'd love to see what your code looks like. You sound like a BASIC
programmer who uses Global variables for everything.
6) If you return a NULL, it is necessary to test for NULL or expect the
code to crash. If there can never be a case where an invalid object can be
returned then in the case of failure, the returned object can provide the
failure behaviour. Once again, I guess that that is too complex a concept
for you. You would rather crack nuts with a sledge hammer.
The point of failure should be as close as possible to the point where the
error occurs. Your method does not give you that. It gives you a false
sense of robustness which your way of doing it does not give. If you like
sugar-coated security blankets which cause more problems than they solve,
be my guest.
However, you've proved repeatedly that you don't know your ass from your
elbow when it comes to C++ programming, so why don't you shut up and go
learn some more before criticizing those who are more experienced than you
and who know more than you do.
7) Exceptions are very costly in resources and further more many operating
systems do not support them (Include Windows CE here). Even where
exceptions are available they should only be used in exceptional
circumstances.
Windows CE does indeed support exceptions - either using the SEH mechanism,
or using C++'s exceptions.
http://msdn.microsoft.com/msdnmag/issues/02/07/WindowsCENET/
How do you know that exceptions are very costly, Ian? You do know that if
exceptions are not fired, they don't cost anything at all, don't you? Maybe
a little code here and there to do stack unwinding, but nothing that you
don't already get with SEH.
By the way, just what IS your definition of an "exceptional circumstance"?
The printer is on fire? Nuclear war? The coming of the four horsemen of the
apocalypse? The Rapture? The Rupture?
8) Good real-times fail over a problem and then carry on. There is
generally no operator there to enjoy the BSOD which you initiated.
Really? You must have a different definition as to what constitutes a
"good" real time system than I do then. In my world, when I can't correct
deserialize data from a file, I have to stop. I can't just say "Oh well, I
probably didn't need that data anyway" and carry on. That way you end up
with a piece of silicon that's only good for use as a hand-warmer.
When errors occur, you need to log them and/or report them. You can't let
other code assume that everything is still hunky dory. That's fine for
parsers - it's not fine for critical systems.
9) I understand fully how new operates, but every time you create an
object it costs time and resources. More over ever object on the heap is a
potential memory leak if things go wrong.
Ah! You ARE a glorified BASIC programmer. Everything IS a statically
allocated global variable to you.
10) As I've pointed out the parameterized factory makes the great core for
a state machine. I suggest that you look at Gamma p114 and you see that
the pattern is used for creating the new state for some form of game, in
the Sample Code.
Good for them. Now look at the section where the Factory pattern is
introduced, idiot.
There are also other important things to be remembered with any code being
written.
a) Within the lifetime of any complex system there are likely to be a
number of changes in the environment in which it needs to work. It is
therefore important not to write code that can only be run on one platform.
.... unless you need to approach the limits of the capabilities of that
platform, in which case it's highly warranted.
I'd love to see you write an interrupt handler for a Coldfire CPU that'll
also run on a Z80.
b) Code will always be asked to do more than it was originally designed
for. It is hence important to not waste resources as they will probably be
needed by the live system.
It is nice to see that you have a alot of room for improvement. It is
never too late to start.
Ian, I'm putting this nicely:
You don't know shit.
Go learn more about C++ programming. Obviously you started learning at some
point and then stopped. You obviously don't know much about the real world
at all.