Francesco said:
vertices = 3;
vertex = new Vertex*[vertices];
You've made your code even more dangerous. Now it's not only using
pointers (which you still haven't checked the validity of), it's also
using dynamic memory (odds are you will either fail to delete it, or do
so incorrectly).
From what I understand, checking the validity means checking that they are
not NULL, or not 0.
Yes. NULL and 0 are the same thing (but when converted to a pointer, the
result need not be all-bits-zero).
What if I make a precondition of this constructor that those pointers must
be valid?
It would still be wise to check that the pre-condition is met. Note that
references cannot be null, so this would be unnecessary if you used
references instead of pointers.
The reason I am asking this is because this is a constructor. If
any of those pointers is null, well I can't leave the constructor and
"return" something.... Should I make the constructor doing nothing at all
and moving this whole business into a setVertices function??
The proper way to signal an error during construction is to throw an
exception. Actually, this is usually the proper way to indicate an error
in any kind of function.
Actually I did put a delete[] vertex inside the destructor,
Good... did you also provide a copy constructor and copy assignment
operator that properly handles self-assignment? If not, you are headed
for trouble. See the "rule of three." It should be in the FAQ, or you
should be able to find many references to it in the archives for this group.
None of this would be necessary however, if you avoided pointers. Or
but I also put a
print statement there to check whether my program is going there when it
closes and I have never seen that printout coming on the screen. That makes
me feel like the object has not been destroyed.
It should be, but it might depend on several things. First, how is the
object allocated? If it is 'new'ed, it obviously has to be 'delete'ed in
order to be destroyed. If it's an automatic local variable, it will be
deallocated when it goes out of scope. However, that can be bypassed if
your program terminates by using functions like exit() or abort().
If none of that describes your program, I'd say the most likely problem
is that your output statement in the destructor fails to properly
terminate the output with a newline (either via '\n' or endl). That can
cause output to not be displayed.
-Kevin