-
January 12th, 2009, 05:14 AM
#1
Why Non Member Function cannot declared with const ?
Hello to all expert, why non MF cannot declared together with constant ?
void NMF()const; ---> Compile error
void MF()const; ----> OK
Thanks for your explanation.
Thanks for your help.
-
January 12th, 2009, 05:48 AM
#2
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?
- Guido
-
January 12th, 2009, 02:13 PM
#3
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.
In code:
Code:
// Equivalent non-const functions
void MyClass::foo()
{
// ...
}
void foo(MyClass * this_ptr)
{
// ...
}
// Equivalent const functions
void MyClass::bar() const
{
// ...
}
void bar(const MyClass * this_ptr)
{
// ...
}
- Alon
-
January 12th, 2009, 11:09 PM
#4
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.
Thanks.
Thanks for your help.
-
January 12th, 2009, 11:36 PM
#5
Re: Why Non Member Function cannot declared with const ?
Originally Posted by Peter_APIIT
Do we still can applied const to non MF at the following code ?
You can apply the const by making the parameter const:
Code:
void NonMemberFUNCtion(const a&);
-
January 12th, 2009, 11:52 PM
#6
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
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.
Please, correct me if I'm wrong.
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|