I have this code:
#include <stdio.h>
class A
{
public:
int doit()
{
return 5;
}
int doit2() const
{
i++;
return i;
}
int i;
};
int main()
{
A a;
printf("%d\n", a.doit() );
return 0;
}
Which compiles with g ++ -Wall -Wpedantic main.cpp. Is there a way to get g ++ to say: "A :: doit () should be marked as const"? g ++ 4.8 has -Wsuggest-attribute = const, but in this case it does not work. g ++ -Wall -Wpedantic -Wsuggest-attribute = const const_main.cpp -fipa-pure-const -O2 -Wextra is still clean.
, const - , , , - , , , , const . , , , const, , . , const const, , , , .
non-const doit2(), const, :
const_main.cpp: In member function ‘int A::doit2() const’:
const_main.cpp:12:6: error: increment of member ‘A::i’ in read-only object
i++;
^
( , const, ).
: ++