const member error
-
I got a doubt with const signature from the below code.
can anyone help me to fix this error?
While I compiled the code, it gave below error. Can inline or static data member function be made const?
error: static member function 'static void ABC::fn_2(const int*)' cannot have cv-qualifierConsider 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?
-
I think this class isn't made for compilation but is part of an assignment. The question is just included below the code.
Which of these functions is a typical const member function signature?
The error message tells you, where to find the problem... For example, you could remove the
const
fromfn_2
.
-
@tampere2021 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();
-
@tampere2021 I guess below line is the final const that makes it a const member.
ABC fn_1(const int*) const
-
@tampere2021 sagte in const member error:
inline ABC const fn_3();
This is simply not a const function The const has to be on the right side of the function signature.
Inline is another topic and has no effect on the constness.
-
@Schlangenmensch I guess this is the right const member function. If I modify the code by removing const, then it compiles fine.
static void fn_2(const int* const) const; //original code
static void fn_2(const int* const); //modified code
-
@tampere2021 I guess below line is the final const that makes it a const member.
ABC fn_1(const int*) const
-
@tampere2021 I guess, your guess is correct.
-
@Schlangenmensch do you mean to say this line guess is correct?
ABC fn_1(const int*) const
-
class ABC { public: ABC fn_1(const int*) const; };
This is a class with a const member function which returns an object of the class itself.
-
-
@Schlangenmensch sagte in const member error:
class ABC { public: ABC fn_1(const int*) const; };
This is a class with a const member function which returns an object of the class itself.
... without modifying members of the instance.
-
@VLSI_Akiko Thanks a lot, I am clear now.
-
Dieser Beitrag wurde gelöscht!