I got a doubt with const signature from the below code.
can anyone help me correct my understanding here?
1 2 3 4 5 6 7 8 9 10 11 12
Consider the following class.
class ABC
{
public:
const ABC fn_0();
ABC fn_1(constint*) const;
staticvoid fn_2(constint* const) const;
inline ABC const fn_3();
};
Which of these functions is a typical const member function signature?
If you had compiled your code, you could have received something like:
error: static member function 'static void ABC::fn_2(const int*)' cannot have cv-qualifier
Which of these functions is a typical const member function signature?
Are you on some multiple-choice quiz?
There is no "typical", only valid and invalid.
Your code has one const member function.
One, as already made clear, is plain syntax error.
The other two are not const.
My bad....I can see that in this function, const int* const is a constant pointer to constant integer
This means that the variable being declared is a constant pointer pointing to a constant integer. Effectively, this implies that a constant pointer is pointing to a constant value. Hence, neither the pointer should point to a new address nor the value being pointed to should be changed.
Ok. does that mean const cant be used with static member as well as inline member functions? My understanding is that Static member functions cannot be const. As inline function will replace the actual call from code, there is no use of calling inline function as const here.
Hence below two lines are invalid. Any suggestions?
The const when applied to a member function (after the () )means that the function does not change the value of any of the class members. In other words it does not change the state of the object. This might be the source of the confusion, if only you would read the documentation linked above.
Other non static functions can still be const. This means the function returns a const value or object. The const is written before the function name. constexpr functions can only call other const functions. So the second example above is not invalid. inline is only a hint to the compiler to inline the function.
Of course. You have valid code when you don't try to make static member function const.
[EDIT]
1 2 3 4 5 6 7
#define foo const
#define bar const
#define gaz const
struct Sample {
T foo fun2 ( U bar ) gaz;
};
The gaz is the const that qualifies the fun2, makes the member function const.
The foo qualifies the type of the value that the function returns. Unrelated to whether the function is const.
The bar qualifies the type of the parameter that the function takes. Unrelated to whether the function is const.