title: TIP: in C++, having a method where "this" is not used is not good design
body: <p>Under the hood, a C++ method of a class is like a C function whose first parameter is an instance of the class - or struct.</p>
  
  <p>For example:</p>
  
  <pre><code>void Foo::Do();
  </code></pre>
  
  <p>would be equivalent to this declaration in C:</p>
  
  <pre><code>void Do(Foo* this);
  </code></pre>
  
  <p>Hence, using a member m_someMember from within a method is like using this->m_someMember from inside the C function.</p>
  
  <p>After so many years of C/C++ programming experience, I just recently asked myself: <i>What if I call a method from an instance pointer that is NULL???</i></p>
  
  <p>My guess was: <i>If the method refers to no member at all, when why would it crash?</i></p>
  
  <p>So I did a quick test (on a Windows platform, with Visual C++ 2008):</p>
  
  <pre><code>class Foo
  {
  public:
      Foo() {}
      virtual ~Foo() {}
  
      void Do();
  };
  
  void Foo::Do()
  {
      cout &lt;&lt; "Calling 'Do' for " &lt;&lt; this &lt;&lt; endl;
  }
  
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      Foo foo;
      foo.Do();
  
      Foo* pNullFoo = 0;
      pNullFoo-&gt;Do();
  
      return 0;
  }
  </code></pre>
  
  <p>Which gives an output like:</p>
  
  <pre><code>Calling 'Do' for 0038FE5C
  Calling 'Do' for 00000000
  </code></pre>
  
  <p>This could be an hassle when doing post-mortem debugging of a crash over an instance pointer that is null. You might think that this method cannot be called if this is invalid.</p>
  
  <p>On the other hand, if the method is declared virtual:</p>
  
  <pre><code>virtual void Foo::Do() { ... }
  </code></pre>
  
  <p>Then the line:</p>
  
  <pre><code>pNullFoo-&gt;Do();
  </code></pre>
  
  <p>will produce an page fault exception. Why? Because instances of a class with virtual methods have a pointer to the vtable to the child class virtual methods they belong to.
  So the first thing the compiler would do is to make pNullFoo to access its vtable member, then bang!</p>
  
  <p>In conclusion, this is better design to have non contextual functions like Do be implemented as procedural routines than methods, unless they are virtual.</p>