const member fn

Sep 27, 2021 at 2:04pm

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?

Sep 27, 2021 at 3:23pm
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 :)
Sep 27, 2021 at 6:45pm
Ok, so the const signature in above code is ABC fn_1(const int*) const?
Sep 29, 2021 at 10:45am
Yep. It's that final const that makes it a const member.
Sep 29, 2021 at 12:05pm
@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.
Sep 29, 2021 at 1:00pm
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
Sep 29, 2021 at 3:05pm
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 Sep 29, 2021 at 3:09pm
Sep 29, 2021 at 3:56pm
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.
Sep 29, 2021 at 5:41pm
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.



Sep 30, 2021 at 5:50am
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;
};
Sep 30, 2021 at 6:28am
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 Sep 30, 2021 at 6:35am
Sep 30, 2021 at 7:16am
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.
Sep 30, 2021 at 7:59am
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.
Sep 30, 2021 at 8:13am
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
Sep 30, 2021 at 9:58am
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 Sep 30, 2021 at 10:07am
Topic archived. No new replies allowed.