Why Non Member Function cannot declared with const ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Why Non Member Function cannot declared with const ?

  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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?

  3. #3
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    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

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: Why Non Member Function cannot declared with const ?

    Quote 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&);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Why Non Member Function cannot declared with const ?

    Quote Originally Posted by Peter_APIIT View Post
    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.

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center