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 difference between Structure and Unions?
Are pointers integer?
What is scanf_s in c?
int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;
How #define works?
shorting algorithmS
How can a program be made to print the name of a source file where an error occurs?
What is the difference between malloc() and calloc() function in c language?
What is c language & why it is used?
What is const and volatile in c?
What is storage class?
Explain about the constants which help in debugging?
c language interview questions & answer
Which driver is a pure java driver
Explain what are multibyte characters?