Search code examples
c++static-membersfunction-qualifier

How can you decrement a static data member from inside a const member function?


class AccountManager
{
private:
    Account accountlist[100];
    int *accountNumber;
    Account* SuperVipAccount; 
    static int ManagerNumber;
public
    int getManagerNumber() const;
};

I have a class like this, and I want to use decrement operator in "getManagerNumber" to let ManagerNumber minus one, what should I do?


Solution

  • ManagerNumber is a static member of the AccountManager (shared across the class and not per object), so you can decrement it very well.
    Method's const correctness doesn't apply to the static members.

    int getManagerNumber() const
    {
      -- ManagerNumber;  // ok
      return ManagerNumber;
    }