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


Please Help Members By Posting Answers For Below Questions

Why is extern used in c?

612


Are local variables initialized to zero by default in c?

550


What does the error message "DGROUP exceeds 64K" mean?

727


What could possibly be the problem if a valid function name such as tolower() is being reported by the C compiler as undefined?

746


What is a stream in c programming?

596






What are the different data types in C?

729


List the difference between a While & Do While loops?

639


Does free set pointer to null?

563


What is pragma in c?

629


find out largest elemant of diagonalmatrix

1651


Explain the use of 'auto' keyword in c programming?

681


the portion of a computer program within which the definition of the variable remains unchanged a) mode b) module c) scope d) none

647


How can you tell whether a program was compiled using c versus c++?

619


What is substring in c?

641


How can variables be characterized?

1652