class foo {
public:
static int func(const char*& p) const;
};
This is illegal, why?
Answer Posted / rohit
the 2nd const used in the example is invalid because it can
be used only with member functions which have a hidden
argument called this. The 2nd const would be applied to
this.
The funct is static member function so it hasn't any this
pointer.
Actually it is not obvious what is exactly inccorect.
We can remove static keyword, then we get syntactically
correct class definition, or we can remove 2nd const and
again the class can be considered valid.
A member function can be declard as Const by considering
the fact that they contain a hidden "this pointer" to be a
pointer to a const object. However Static methods do not
have the "this pointer", and hence can't be const or
virtual.
Hence it is illegal to declare a static function as const.
The C++ language standard stipulates at section 9.4.1
that "[...] A static
member function shall not be declared const
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
What is the difference between pure virtual function and virtual function?
Are comments included during the compilation stage and placed in the EXE file as well?
What is a void pointer in c?
How will you write a code for accessing the length of an array without assigning it to another variable?
Can variables be declared anywhere in c?
What is the usage of the pointer in c?
Is null always equal to 0(zero)?
What is difference between array and pointer in c?
Does free set pointer to null?
What is an lvalue?
What is auto keyword in c?
What does emoji p mean?
Why is %d used in c?
How to delete a node from linked list w/o using collectons?
What is function prototype in c with example?