On 2007-09-11 08:09, love307 wrote:
> Hi all!
> I have one question, want to have your help for answer it.
>
> This is a problem:
> class A
> {
> public:
> int n;
> void printMessage()
> {
> printf("hello world");
> }
> };
>
> void main()
> {
> A *a = NULL;
> a->printMessage();
> }
>
> I don't know why when debug: a = 0x0000000;
> but we still call printMessage function.
> Thank for your help!
As Kai-Uwe Bux explained this is undefined behaviour, the "problem" with
your code is that printMessage() does not access any members of the
class, if you modify it to something like
void printMessage()
{
printf("n: %d", n);
}
you will get your expected crash. In general, a function that does no in
any way access another member of the class should probably be static.
--
Erik Wikström