title: Using "Static" Keyword to Limit Access in C++ Member Functions
body: <p>I understand that one benefit of having static member functions is not having to initialize a class to use them.  It seems to me that another advantage of them might be not having direct access to the class's not-static stuff.</p>
  
  <p>For example a common practice is if you know that a function will have arguments that are not to be changed, to simply mark these constant. e.g.:</p>
  
  <pre><code>bool My_Class::do_stuff(const int not_to_be_changed_1,
                          std::vector&lt;int&gt; const * const not_to_be_changed_2)
  {
    //I can't change my int var, my vector pointer, or the ints inside it.
  }
  </code></pre>
  
  <p>So is it valid to use static member functions to limit access.  For example, lets say you have a function</p>
  
  <pre><code>void My_Class::print_error(const unsigned int error_no) {
    switch (error_no) {
      case 1:
        std::cout &lt;&lt; "Bad read on..." &lt;&lt; std::endl;
        break;
      //...
      default:
        break;
    }
  }
  </code></pre>
  
  <p>Well here we're not going to be accessing any member variables of the class.  So if I changed the function to:</p>
  
  <pre><code>static void My_Class::print_error(const unsigned int error_no) {
    switch (error_no) {
      case 1:
        std::cout &lt;&lt; "Bad read on..." &lt;&lt; std::endl;
        break;
      //...
      default:
        break;
    }
  }
  </code></pre>
  
  <p>I'd now get an error, if I inadvertently tried to access one of my private var, etc. (unless I pass myself an instance of my class, which would be purposeful ^_^  !)</p>
  
  <p>Is this a valid technique, similar to proactively making args that should not be changed constants?</p>
  
  <p>What downsides might it have in terms of efficiency or use?</p>
  
  <p>My chief reason for asking is that most of the "static" tutorials I read made no mention of using it in this way, so I was wondering if there was a good reason why not to, considering it seems like a useful tool.</p>
  
  <hr>
  
  <p><strong>Edit 1</strong>: A further logical justification of this use:</p>
  
  <p>I have a function print_error,as outlined above.  I could use a namespace:</p>
  
  <pre><code>namespace MY_SPACE {
     static void print_error(...) {
        ...
     } 
  
     class My_Class {
        ....
        void a(void)
     }
  }
  </code></pre>
  
  <p>But this is a pain, because I now have to lengthen ALL of my var declarations, i.e.</p>
  
  <pre><code>MY_SPACE::My_Class class_1;
  </code></pre>
  
  <p>all to remove a function from my class, that essentially <em>is</em> a member of my class.</p>
  
  <p>Of course there's multiple levels of access control for functions:</p>
  
  <pre><code>//can't change pointer to list directly
  void My_Class::print_error(std::vector&lt;int&gt; const * error_code_list) {...}
  
  //can't change pointer to list or list members directly
  void My_Class::print_error(std::vector&lt;int&gt; const * const error_code_list) {...}
  
  //can't change pointer to list or list members directly, access
  //non-const member vars/functions
  void My_Class::print_error(std::vector&lt;int&gt; const * const error_code_list) const {...}
  
  //can't change pointer to list or list members directly, access
  //non-static member vars/functions
  static void My_Class::print_error(std::vector&lt;int&gt; const * const error_code_list) {...}
  
  //can't change pointer to list or list members directly, access
  //member vars/functions that are not BOTH static and const
  static void My_Class::print_error(std::vector&lt;int&gt; const * const error_code_list) const {...}
  </code></pre>
  
  <p>Sure this is a bit atypical, but to lessening degrees so are using const functions and const variables.  I've seen lots of examples where people could have used a const function, but didn't.  Yet some people think its a good idea.  I know a lot of beginning c++ programmers who wouldn't understand the implications of a const function or a static one.  Likewise a lot would understand both.</p>
  
  <p>So why are some people so adamantly against using this as an access control mechanism if the language/spec provides for it to be used as such, just as it does with const functions, etc.?</p>