Re: Calling a non-virtual member function for a NULL instance
Victor Bazarov wrote:
James Kanze wrote:
Victor Bazarov wrote:
Matt Curtis wrote:
I understand the following code is illegal:
class X {
public:
void f();
};
X * x = NULL;
x->f();
You understand incorrectly. It has undefined behaviour,
and only illegal in some countries, where undefined
behaviour is outlawed.
According to the standard, undefined behavior is illegal code
Please quote the passage from the Standard that says that.
?1.3.12. The definition of undefined behavior. (The standard
uses the word "erroneous", rather than "illegal", but this is
true in most other places as well.)
Arguably, of course, it's not an illegal C++ program, because it
is no longer a C++ program. But I'm not into those sort of word
games -- we're talking about C++ here. If you use undefined
behavior which the platform defines in non-portable code, that's
perfectly acceptable -- I don't think that you can write a
serious application without doing so. (Note that threads, GUI
and sockets are all undefined according to the C++ standard.)
In this case, however, I've never seen nor heard of an
implementation which defines the behavior, except one debugging
implementation which guaranteed that the program aborted with an
error message if you did this.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]