I have code like this…
class Time
{
public:
Time(int, int, int);
void set_hours(int);
void set_minutes(int);
void set_seconds(int);
int get_hours() const;
int get_minutes() const;
int get_seconds() const;
static void fun() ;
void printu() const;
void prints();
private:
int x;
int hours;
int minutes;
int seconds;
const int i;
};
Why do I need to put const at last to make a function constant type but if i need to make a function, I can do this like…
static void Time::fun()
{
cout<<"hello";
}
Above function fun() is also in same class. I just want to know what is the reason behind this?
with a const instance method such as
int get_hours() const;, theconstmeans that the definition ofint get_hours() const;will not modifythis.with a static method such as
static void fun();, const does not apply becausethisis not available.you can access a static method from the class or instance because of its visibility. more specifically, you cannot call instance methods or access instance variables (e.g.
x,hours) from the static method because there is not an instance.