0

I want to be able to have a method that returns something and cannot change member variables like in a normal class, for example:

class blah
{
   const int& Get_X() const
   {
       return m_x;
   }

private:
    int m_x = 0;
};

but in my singleton class, it won't let me compile something like this. It gives me this error message:

error: static member function 'static const SDL_Point& Events::Mouse_Pos()' cannot have cv-qualifier

the code in the header file is this:

static const SDL_Point& Mouse_Pos() const;

because I want to be able to (in the .cpp file) declare something such as

const SDL_Point& Events::Mouse_Pos() const
{
    return Get_Instance().m_Mouse_Pos;
}

What am I missing? Or can you not do this?

  • That doesn't answer my question, I still don't know how to get the code to allow me to do this (or why it wont). It doesn't make sense to me why you can't have this? Regardless of `this` you should still be able to have a method that stops you from changing member variables static or not? –  Feb 07 '21 at 00:46
  • The very first answer to the other question is an answer to your question. Static class methods have no `this`, hence it makes no sense for static class methods to be `const`. That's not how C++ works. – Sam Varshavchik Feb 07 '21 at 00:54
  • If it's a singleton, you can just make them const member functions and call them from the statically accessible single instance. While I agree it would be nice to be able to declare a `static const` function that doesn't change any static variables of a class, C++ doesn't support doing so. – Nathan Pierson Feb 07 '21 at 01:03

0 Answers0