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
Why is extern used in c?
Are local variables initialized to zero by default in c?
What does the error message "DGROUP exceeds 64K" mean?
What could possibly be the problem if a valid function name such as tolower() is being reported by the C compiler as undefined?
What is a stream in c programming?
What are the different data types in C?
List the difference between a While & Do While loops?
Does free set pointer to null?
What is pragma in c?
find out largest elemant of diagonalmatrix
Explain the use of 'auto' keyword in c programming?
the portion of a computer program within which the definition of the variable remains unchanged a) mode b) module c) scope d) none
How can you tell whether a program was compiled using c versus c++?
What is substring in c?
How can variables be characterized?