class foo {
public:
static int func(const char*& p) const;
};

This is illegal, why?

Answers were Sorted based on User's Feedback



class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / rafal dzbek

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.

Is This Answer Correct ?    3 Yes 0 No

class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / jaroosh

This code is obviously wrong, and here is why :
declaring a method to be const, means :
this method cannot CHANGE values of any member variables,
but while the method already is static, it has no means of
changing values of member variables, because simply - it
cannot see them (its belongs to a CLASS, not any specific
OBJECT).

Is This Answer Correct ?    3 Yes 0 No

class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / abdur rab

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 ?    3 Yes 0 No

class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / revathy

static functions can access static data only
Thus this is illegal

Is This Answer Correct ?    1 Yes 1 No

class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / john gummadi

"const" cannot be used along with reference. And the function itself is const which means it cannot change any parameters. So no place for the reference here. If you really need reference, then get id of consts.

Is This Answer Correct ?    0 Yes 0 No

class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / john gummadi

There is nothing to do with class members here, we don't
see any. The function takes a constant pointer as a
parameter, when it is constant it cannot be changed
(although technically we can change by casting), then you
cannot use reference (&).

But who knows, compilers may accept, I haven't tested.

Is This Answer Correct ?    1 Yes 2 No

class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / 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

class foo { public: static int func(const char*& p) const; }; This is illegal, why?..

Answer / kalpana.y

this is illegal because

->here the class name is foo
->but,func is declared
->const is declared at outside

Is This Answer Correct ?    0 Yes 4 No

Post New Answer

More C Interview Questions

Can a file other than a .h file be included with #include?

0 Answers   Aspire, Infogain,


int far *near * p; means

0 Answers   Honeywell,


How to find the digits truncation when assigning the interger variable to the character variables. like int i=500; char x = i : here we have truncation. how to find this. another ex: i =100; char x=i. here we do not have truncation.

1 Answers   HCL,


Which header file should you include if you are to develop a function which can accept variable number of arguments?

0 Answers   TCS, TISL,


#define min((a),(b)) ((a)<(b))?(a):(b) main() { int i=0,a[20],*ptr; ptr=a; while(min(ptr++,&a[9])<&a[8]) i=i+1; printf("i=%d\n",i);}

3 Answers  






const char * char * const What is the differnce between the above tow?.

6 Answers   Ramco, TCS,


Explain what are the advantages and disadvantages of a heap?

0 Answers  


Differentiate between #include<...> and #include '...'

0 Answers  


Write a program to remove the C comments(/* */) and C++ comments(//) from a file. The file should be declared in command line.

4 Answers   Persistent, Subex,


What is c standard library?

0 Answers  


What are the scope of static variables?

0 Answers  


#include<stdio.h> #include<conio.h> int main() { int a[4][4]={{5,7,5,9}, {4,6,3,1}, {2,9,0,6}}; int *p; int (*q)[4]; p=(int*)a; q=a; printf("\n%u%u",p,q); p++; q++; printf("\n%u%u",p,q); getch(); return 0; } what is the meaning of this program?

2 Answers  


Categories