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

What is difference between Structure and Unions?

644


Are pointers integer?

554


What is scanf_s in c?

637


int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;

1129


How #define works?

624






shorting algorithmS

1805


How can a program be made to print the name of a source file where an error occurs?

735


What is the difference between malloc() and calloc() function in c language?

609


What is c language & why it is used?

588


What is const and volatile in c?

571


What is storage class?

658


Explain about the constants which help in debugging?

858


c language interview questions & answer

1465


Which driver is a pure java driver

996


Explain what are multibyte characters?

631