Static memfun through null ptr

O

Oliver Anhuth

Hello all,

is it legal to call a static member function with a null pointer?

Is this correct C++?

class X {
public:
static void f() {}
};

int main() {
X* pX = 0;
pX->f();
}

Thanks Oliver
 
G

Gianni Mariani

Oliver said:
Hello all,

is it legal to call a static member function with a null pointer?

Is this correct C++?

class X {
public:
static void f() {}
};

int main() {
X* pX = 0;
pX->f();
}

Thanks Oliver

No, it is not legal.

The behaviour is undefined.
 
R

Ron Natalie

Oliver Anhuth said:
Hello all,

is it legal to call a static member function with a null pointer?
No. It is illegal to dereference, that is apply * or -> to a null pointer
even if the result would appear not to care about the actual value of
the pointer.
 
H

Howard

Oliver Anhuth said:
Hello all,

is it legal to call a static member function with a null pointer?

Is this correct C++?

class X {
public:
static void f() {}
};

int main() {
X* pX = 0;
pX->f();
}

Thanks Oliver

No, it's not legal, but if it's static then you don't need an instance
variable at all. Just call it like this:

X::f();

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top