Home

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

previous
next

Re: code for number guessing by computer
Re: Module imports during object instantiation
Re: Python end of file marker similar to perl's __END__
Re: Impersonate another user temporarily (Unix and Windows)
Re: Local Functions...
Pajacyk
Krwinka
Niechciane i Zapomniane
Mimo Wszystko
Nasze Dzieci