Reputation: 1015
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?
Upvotes: 14
Views: 17129
Reputation:
It is because that if you put the const first it would mean that you are returning a const thing from the function
Upvotes: 1
Reputation: 792907
It's a purely grammatical issue. const
is a cv-qualifier
and, when applied to a member function, must be placed after the parameter declarations. It you attempted to place it before the function name it could only be interpreted as qualifying the return type of the function.
static
, on the other hand, is a storage class specifier and must appear before the declarator to which it applies.
These rules just flow from the way the C++ grammar is defined.
Upvotes: 1
Reputation: 104708
with a const instance method such as int get_hours() const;
, the const
means that the definition of int get_hours() const;
will not modify this
.
with a static method such as static void fun();
, const does not apply because this
is 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.
class t_classname {
public:
static void S() { this->x = 1; } // << error. this is not available in static method
void s() { this->x = 1; } // << ok
void t() const { this->x = 1; } // << error. cannot change state in const method
static void U() { t_classname a; a.x = 1; } // << ok to create an instance and use it in a static method
void v() const { S(); U(); } // << ok. static method is visible to this and does not mutate this.
private:
int a;
};
Upvotes: 18
Reputation: 151720
One and another is explained a bit more in detail here. Putting const
after a function declaration makes the function constant, meaning it cannot alter anything in the object that contains the function.
Upvotes: 0
Reputation: 49261
The const in the end means the function is constant, so it doesn't change the object's state.
When you put the const in the end, you can't change the state of the object's members.
Declaring a function static means it doesn't belong to the object at all, it belongs to the class type.
Putting const in the beginning means the return type value is a constant.
Upvotes: 16
Reputation: 15343
When you put const at the beginning, you're applying it to the return type. This doesn't matter if you're return type if void, but lets say you're returning char*
that is not const. If you put const at the beginning, you'd end up with
static const char* MyFunction() { ... }
That tells me that the return type is a const char*
, not a const function that returns a char*
.
Putting it at the end avoids this problem.
Upvotes: 4
Reputation: 60027
It is because that if you put the const first it would mean that you are returning a const
thing from the function - i.e. a different meaning that the function is const
Upvotes: 2