Explain me are you allowed to have a static const member function?
Submitted by: MuhammadA const member function is one which isn't allowed to modify the members of the object for which it is called. A static member function is one which can't be called for a specific object.
Thus, the const modifier for a static member function is meaningless, because there is no object associated with the call.
A more detailed explanation of this reason comes from the C programming language. In C, there were no classes and member functions, so all functions were global. A member function call is translated to a global function call. Consider a member function like this:
void foo(int i);
A call like this:
obj.foo(10);
…is translated to this:
foo(&obj, 10);
This means that the member function foo has a hidden first argument of type T*:
void foo(T* const this, int i);
If a member function is const, this is of type const T* const this:
void foo(const T* const this, int i);
Static member functions don't have such a hidden argument, so there is no this pointer to be const or not.
Submitted by: Muhammad
Thus, the const modifier for a static member function is meaningless, because there is no object associated with the call.
A more detailed explanation of this reason comes from the C programming language. In C, there were no classes and member functions, so all functions were global. A member function call is translated to a global function call. Consider a member function like this:
void foo(int i);
A call like this:
obj.foo(10);
…is translated to this:
foo(&obj, 10);
This means that the member function foo has a hidden first argument of type T*:
void foo(T* const this, int i);
If a member function is const, this is of type const T* const this:
void foo(const T* const this, int i);
Static member functions don't have such a hidden argument, so there is no this pointer to be const or not.
Submitted by: Muhammad
Read Online C++ Programmer Job Interview Questions And Answers
Top C++ Programmer Questions
☺ | Explain me what is an Object/Instance? |
☺ | Explain void free (void* ptr)? |
☺ | Tell me how to create a pure virtual function? |
☺ | Tell me what will the line of code below print out and why? |
☺ | Please explain is there a difference between class and struct? |
Top C Plus Plus Language Categories
☺ | C++ Pointers & Functions Interview Questions. |
☺ | C++ Operator Overloading Interview Questions. |
☺ | C++ Exception Handling Interview Questions. |
☺ | C++ Template Interview Questions. |
☺ | C++ Virtual Functions Interview Questions. |