Reputation: 313
I want to ask a simple question
Like for example in my private member I have declared static member.
static int id;
and in the public I have used getter function for this id
static int getID() const;
The compilor is giving me an error but when I dont use const it does not give any error, because this is only getter it should be constant, please tell me the reasons.
Upvotes: 0
Views: 65
Reputation: 29724
This is a static
function which cannot be const
, because it doesn't act on any particular instance of class. This means such function has no this
pointer (passed implicitly as hidden argument) to any particular instance. You should write
static int id;
static int getID();
It is also possible to make this function non static
int getID() const;
however such a function in general should be static, as long as it doesn't need access to specific object's representation.
Upvotes: 1