Keyword const in class declarations [duplicate] - c++

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 7 years ago.
I understand that the keyword const marks variables, parameters etc as read only. In a class declaration why is const used at the end of a member declaration, for example:
QString getClassName() const;

Declaring a method const means that the method won't change the state of the object and thus it can be called on const instances of the class.
Notice that const-ness is enforced by the compiler. const methods can't modify member variables unless they are declared mutable. It's very common to have mutable locks so that const methods can still be synchronized.

It basically means that the function is "promising" that it won't change the calling object.

Related

Copy paste of const and non-const methods [duplicate]

This question already has answers here:
How do I remove code duplication between similar const and non-const member functions?
(21 answers)
Closed 2 years ago.
Consider the following code:
http://coliru.stacked-crooked.com/a/d89377889a8ff749
IStorage has const and non-const get methods.
The concrete implementation defines them, but the definition is just copy-paste.
I could not call one get method from another because of the const mechanics of C++.
Is there any way to avoid this copy-paste?
This is one of the legitmate uses of const_cast
const std::string* get(ID id) const override
{
return const_cast<ConcreteStorage*>(this)->get(id);
}

static member vs static const member instantiation - what's the difference? [duplicate]

This question already has answers here:
Why can't I initialize non-const static member or static array in class?
(5 answers)
Closed 7 years ago.
What exactly is the difference between these two members in question of initialization and instantiation?:
class Test {
// ctor, dtor, ... here
private:
static int m_test = 0; // error - non-const ...
static const int m_const_test = 0; // working - const ...
};
Why is initialization of non-const members not valid?
And when will both variables (assuming non-const member without init!) be instantiated?
Edit: Even if a good part of this question is superimposable to the mentioned posts, i don't think that every part of my question is answered in these entries.
BR, Tuna
If multiple compilation units include your class definition then in which one should be your static int m_test = 0 be placed? For const static ... it does not matter because it is const.
m_const_test being declared as const canĀ“t be changed.
So, you can save a little bit of time using m_const_test, but is not as flexible as m_test.

Why the use of const in a method with no parameters? [duplicate]

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
What is meant with "const" at end of function declaration? [duplicate]
(6 answers)
Closed 8 years ago.
why is the purpose of "const" in that case?
std::string List::reqClubName() const
{
return m_Club;
}
Thanks
Banning the modification of members is not the only reason to qualify a member function as const. Whether you want to modify members or not, you can only call a member function on an object through a const context if the member function is marked const:
#include <iostream>
#include <string>
struct List
{
std::string reqClubName()
{
return m_Club;
}
private:
std::string m_Club;
};
int main()
{
const List l;
std::cout << l.reqClubName();
// ^ illegal: `l` is `const` but, `List::reqClubName` is not
}
Neither the language nor the compiler care that reqClubName doesn't try to modify the object anyway; your program will not compile.
Because of this, a const suffix should be your default approach unless you do need to modify a data member.
The const after a member function says that the function does not modify member data in the class it is a part of.

Class syntax explanation required [duplicate]

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 9 years ago.
I am learning C++ in Qt environment and I was going through one of the sample code online.
Can anyone please explain this syntax to me?
const TicTacToe * GetTicTacToe() const { return m_tictactoe.get(); }
Why is there a const before the opening bracket of a function? Is it a pointer or multiplication?
the full class is as follows, but the syntax of the instructions mentioned above is not clear to me
class QtTicTacToeWidget : public QWidget
{
Q_OBJECT
public:
explicit QtTicTacToeWidget(QWidget *parent = 0);
const TicTacToe * GetTicTacToe() const { return m_tictactoe.get(); }
void Restart();
The first const is to signify that the variable pointer TicTacToe can't be changed. The second constant after the function declaration says that anything that happens inside this function will not change any member variable inside the class. Because it effectively does not change memory data on the class, it can be used when you use any constant object of that class. For example:
const QtTicTacToeWidget myConstObject;
// Since myConstObject is a constant, I am not allowed to change anything inside
// the class or call any functions that may change its data. A constant function
// is a function that does not change its own data which means I can do this:
myConstObject.GetTicTacToe();
// But I can not do the next statement because the function is not constant
// and therefore may potentially change its own data:
myConstObject.Restart();
The const between before the opening bracket signifies that the function is a const member function. It essentially says that it guarantees to not modify the class and therefore can be called on an object declared as const.
Well that, and it also allows the function to modify mutable variables in a const class.

Why does this const member function modifies the static member data? [duplicate]

This question already has answers here:
Why can a const member function modify a static data member?
(4 answers)
Closed 5 years ago.
I have the following line of code from Eckel-Thining in C++
Class Obj{
static int i,j;
public:
void f() const {cout<<i++<<endl;}
void f() const {cout<<i++<<endl;}
};
int Obj::i=47;
int Obj::j=11;
Now it's written in Ecekl for const member functions that by declaring a member function const , we tell the compiler to refrain from modifying a class data. I understand that in some specific cases like mutable const and by explicitly casting away constness of this pointer , we can do away with that but here neither of the two are happening and i++ and j++ working fine. Why is it so?
const is only for object (this pointer is const), modifying static members is allowed.
In a const member function, the object for which the function is called is accessed through a const access path; therefore, a const member function shall not modify the object and its non-static data members.
source:someone cites c++ standard
As you can see, static data member is not protected by const as per c++ standard.