const member fn


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(const int*) const;
  static void fn_2(const int* const) const;
  inline ABC const fn_3();
};
Which of these functions is a typical const member function signature?

A const member function, is one in which it is impossible to modify any of the object's data members.

A "typical" signature would not be a static member, nor an inline one.

That should narrow it down for you :)
Ok, so the const signature in above code is ABC fn_1(const int*) const?
Yep. It's that final const that makes it a const member.
@MikeyBoy:
nor an inline one.


Why do you say this? I think getter functions could be perfect examples of inline, const functions.

I fail to see why an inline function would not be a typical const function.
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.


You could read https://en.cppreference.com/w/cpp/language/function
Do you mean to say that below is the correct syntax for const member function,but has compile errors?

static void fn_2(const int* const) const; //correct syntax

Last edited on
Well if it has compile errors it can't be correct syntax, can it!

It's not correct syntax as you can't have a modifier (const here) with a static member function.
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.



Yes, but type of parameter(s) has nothing to do with the topic of whether a member function is const or not.


Since you are obsessed with overloading, here is overloading:
1
2
3
4
5
6
class ABC
{
public:
  ABC fn_1(const int*);
  ABC fn_1(const int*) const;
};
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?

static void fn_2(const int* const) const;
inline ABC const fn_3();
Last edited on
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.
does that mean const cant be used with static member [..] functions? My understanding is that Static member functions cannot be const.

Correct. Static member function cannot be const. However, what const do you refer to in "const cant be used with"?
1
2
3
4
5
class ABC
{
public:
  static ABC const fn_1(int const *); // valid
};


Inline is ... something different. See https://en.cppreference.com/w/cpp/language/inline
Non-static member function can be const, even if it is inlined.
If I modify below line by removing const, then no compile errors are seen.

static void fn_2(const int* const) const; //original code
static void fn_2(const int* const); modified code
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.
Last edited on
Topic archived. No new replies allowed.