Re: Why Non Member Function cannot declared with const ?
Because thereīs no object const correctness can be applied to. If you declare a member function const you promise not to modify the observable state of the object you call the function on. Non member functions do not have an object they belong to, so what do you promise not to change?
Re: Why Non Member Function cannot declared with const ?
In other words, recall that member functions are implicitly passed a this-pointer when called. When you declare a member function as const, you are specifying the constness of that implicit parameter. Free and static functions cannot be declared as such because it would not make any sense; they have no implicit parameters.
Re: Why Non Member Function cannot declared with const ?
Because there´s no object const correctness can be applied to. If you declare a member function const you promise not to modify the observable state of the object you call the function on. Non member functions do not have an object they belong to, so what do you promise not to change?
Agree.
Do we still can applied const to non MF at the following code ?
Code:
class a;
void NonMemberFUNCtion(a&)const;
This non MF exist an object(this pointer/class a) that we can applied constness to it.
Re: Why Non Member Function cannot declared with const ?
Originally Posted by Peter_APIIT
Agree.
Do we still can applied const to non MF at the following code ?
Code:
class a;
void NonMemberFUNCtion(a&)const;
This non MF exist an object(this pointer/class a) that we can applied constness to it.
Thanks.
Hello Peter_APIIT,
I'm tyring to understand what you mean by a non-member function existing on an object to which we can apply constness.
I think you're taking this way too deep.
The const qualifier at the end of the member function simply means the function will not change the state of an object which it belongs to (class scope).
For example, a class object has an observable (internal) state,
while an integral type does not (correct me if I'm wrong).
so when we do
Code:
void foo() const;
inside main() ,
it's like saying foo will not change the internal state of whatever object it belongs to, which in this case is main()? (not too sure about this). That means we can't do anything!
so it's really pointless to allow non member function to have the const at the end.
Bookmarks