Use of NULL pointer: reinterpret_cast<Foo*>(0)->foo();

A

Alex Vinokur

struct Foo
{
void foo()
{
// No data members used
}
};

Is this statment correct?
reinterpret_cast<Foo*>(0)->foo();

Alex Vinoku
 
I

Ian Collins

Alex said:
struct Foo
{
void foo()
{
// No data members used
}
};

Is this statment correct?
reinterpret_cast<Foo*>(0)->foo();

It's safe on many implementations but it isn't correct.
 
B

Bo Persson

Alex said:
struct Foo
{
void foo()
{
// No data members used
}
};

Is this statment correct?
reinterpret_cast<Foo*>(0)->foo();

No, it is undefined behavior to use a null pointer.

Some hardware will likely catch the error, some will not.


Bo Persson
 
R

Ron

No, it is undefined behavior to use a null pointer.

Some hardware will likely catch the error, some will not.

Besides, if "no data members are used" why don't you just
make the foo() function a static member. Then you don't
need a pointer (null or otherwise) to call it?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top