Question:
C++ strange function prototypes?
[ J ] a [ Y ]
2010-04-10 00:04:11 UTC
Hey all. I was just taking a look at some strange function declarations and got a little stuck interpreting them. Can anyone give me a heads up as to what they're really defining?

1. int f( ) const;

2. int g(const A& x);

My initial expectation with the first declaration was that it would actually be invalid, since there isnt anything following the const keyword. But it actually works, and in order to define the function later, I need to proceed the function name with the const keyword again. Whats the point of this??

As for 2, its really ambiguous to my eyes. Any interpretations are welcome.
Three answers:
moviebuff
2010-04-10 00:25:03 UTC
Declaration 1 is indicating to the compiler that method f() is an accessor only, and does not modify any data members in the class that it is associated with. Therefore it is safe to use with a const object, which is an object that should not be modified during program execution.



Declaration 2 states that function g takes as a parameter a reference to an object of class A which is not allowed to be modified by function g. Therefore, any attempt to use a method on object x which could modify the data members of object x will generate a compile error. Tying this in with the definition of method f above (in this case, assuming f() is a method on class A), the compiler would allow the use of method f on object x because the declaration of method f states that it will not modify object x's data members.



If that isn't clear, here is an example:



class A

{

public:

A();

void e(int p);

int f() const;



protected:

int z;

};



A::A()

: z(0)

{};



int A::f() const

{

return z;

}



void A::e(int p)

{

z = p;

}





int g(const A& x)

{

x.e(4); // compile error - method e modifies object x which is not allowed by the "const A& x"

return x.f(); // ok - method f does not modify object x...the compiler knows this by the "const" on f()

}
oops
2010-04-10 00:52:49 UTC
Yeah, that stuff confused me for a while too when I was first learning c++. Moviebuff's answer is correct, but if you need a more in depth explanation, rather than explaining it to you here, I'll give you a link to the same place that made it clear to me.



http://www.parashift.com/c++-faq-lite/const-correctness.html
?
2016-10-25 02:55:46 UTC
you will not define the two functionality yet once you define skill functionality i would not understand that(sorry) and likewise c++ is a favourite language not block favourite means in case you define any functionality it would be out of important and the different functionality as one functionality won't be able to comprise yet another functionality. desire totally this show you how to. i made somewhat alterations on your application. this application does what you decide directly to with out any errors i verify it my own. #include79abd8cf35895c56cc4d955c5355dbe making use of namespace std; double factorial(double x) { double a=a million; on a similar time as(x>a million) { a=a*x; x--; } return a; } long double skill(double x,double y) { long double z=a million; // i assume x to skill y if(y>=0) { on a similar time as(y>0)// basically for valuable powers { z=z*x; y--; } } else { on a similar time as(y<0)// basically for -ve powers { z=z/x; y++; } } return z; } double Num; double reality; double x,y; double pow; double powe=0; int important() { cout<<"enter a huge selection: ";cin>>Num; reality = factorial(Num); cout<<"Factorial = "<>x>>y; cout<<"x^y = "<


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...