0

I think it's best to explain my question on a sample code.

Let's say we have got a class:

class Test{
public:
    void doThings(){
        std::cout << "Hello" << std::endl;
    } // theoritically could be static method, but let's make it non static
private:
    // members not used by doThings method
};

So We have got class Test and method doThings, that we could as well declare static, be let's say we forgot to do this.

Now my question is: Is a code below undefined behavior?

void func(){
    Test* ptr = nullptr;
    ptr->doThings();
}

In my opinion it at least should be, but I do not know what does C++ standard state. On the other hand, compiler should generate completely valid code.

DawidPi
  • 2,285
  • 2
  • 19
  • 41
  • 2
    [Yes](http://stackoverflow.com/a/2727872/2508150), it is *undefined behavior*. – Paolo M Dec 10 '15 at 11:10
  • 1
    A brief note on undefined behavior: the compiler's allowed to do whatever it wants if it detects such behavior, including inexplicably raising SIGFPE unconditionally. And that's if the compiler is being *helpful* enough to grab your attention. – user Dec 10 '15 at 11:12

0 Answers0