Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

main() { printf("hello%d",print("QUARK test?")); }

5 Answers  


Write a program which returns the first non repetitive character in the string?

0 Answers   Expedia,


multiple of 9 without useing +,* oprator

3 Answers  


What is typedef example?

0 Answers  


What are the types of bitwise operator?

0 Answers  


What is C language ?

0 Answers   Jekson,


What is switch in c?

0 Answers  


what is the hexidecimal number of 4100?

16 Answers   Google,


Write a function that accepts a sentence as a parameter, and returns the same with each of its words reversed. The returned sentence should have 1 blank space between each pair of words. Demonstrate the usage of this function from a main program. Example: Parameter: “jack and jill went up a hill” Return Value: “kcaj dna llij tnew pu a llih”

5 Answers   Mind Tree,


/*program to calculate hra,da in salary if salary less than 10000 then hra15%,da13% otherwise hra20%,da18%/*

6 Answers  


What is a good data structure to use for storing lines of text?

0 Answers  


What is the value of y in the following code? x=7;y=0; if(x=6) y=7; else y=1;

12 Answers   TCS,


Categories