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);
}
Related
This question already has answers here:
Why doesn't a const method override a non-const method in C++?
(6 answers)
virtual function const vs virtual function non-const
(3 answers)
Top-level const doesn't influence a function signature
(7 answers)
const qualifier disappears from pure virtual function [duplicate]
(1 answer)
Closed 2 years ago.
Suppose I have a method1 in the parent class and I want to override it in a subclass. Does adding const affect the overriding? For example,
int Subclass::method1(const int a)const {...}
Does this override
int Parentclass::method1(int a) {...}
correctly?
This question already has answers here:
when does a function have to return a reference in c++ [duplicate]
(5 answers)
C++: what is the advantage of references in this case?
(3 answers)
C++ return by reference what are the effects?
(2 answers)
Closed 2 years ago.
I came across the code, where in a class one of the member function returns by reference. What are the advantages of doing it like that? E.g.:
class SomeClass {
std:vector<int> some_vals;
// some functions that modify some_vals
public:
const std::vector<int>& get_some_vals() const { return some_vals; }
};
And then when the function is used:
SomeClass my_obj;
const vector<int>& some_vals = my_obj.get_some_vals();
What is the advantage of doing it like this instead of just returning std::vector?
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.
This question already has answers here:
Adding function to string class
(3 answers)
Closed 8 years ago.
For example, if you have an object of the std::string class, you can call the compare(string str) function as follows:
myString.compare(myOtherString);
How can I make my own function that I can call on a string in the same way? Example:
myString.contains(char[] chars);
You can't add member functions to existing classes in C++. Instead, write a non-member function
bool contains(std::string const & s1, std::string const & s2);
and call it as
contains(myString, myOtherString);
You can't really do this in C++, and you shouldn't try. Sure, you could inherit from std::string and implement your methods, but then your methods will be unusable from regular strings. In C++ we do not try too hard to make everything a class method--free functions are functions too!
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.