Why the below piece of code is not crashing , though i have deleted the object?

后端 未结 10 1281
北海茫月
北海茫月 2021-01-18 02:29
class object
{
  public:
    void check()
    {
      std::cout<<\"I am doing ok...\"<

        
10条回答
  •  终归单人心
    2021-01-18 02:45

    It's even funnier than that. This compiles & runs nicely:

    #include 
    using namespace std;
    class object {
        public:
          void check() {
              cout << "I am doing ok..." << endl;
          }
    };
    
    int main() {
       object *p = (object*)0;
       p->check();
       return 0;
    }
    

    On to the shell:

    $ g++ -o t t.cc
    $ ./t
    I am doing ok...
    $
    

    :) You don't actually have to have an object to call this method! cheers, h

提交回复
热议问题