This question already has answers here:
C++ Return value, reference, const reference
(5 answers)
What is a reference variable in C++?
(12 answers)
Closed 1 year ago.
I am trying to understand the useage of 'const' and '&' in the following function declaration. I know that the last 'const' means the function cannot change member variables in the class and that 'const std::string& message' means the variable passed to the function cannot be changed, but I don't understand the meaning of 'const Logger&'. What is the purpose of this first 'const' and why is there an '&' following 'Logger'? Is this function meant to return an address or a pointer?
const Logger& log(const std::string& message) const;
So const Logger& is the return type of log. const means you will not be able to edit the return value at all. The return type Logger& means you'll get a reference to a Logger and not a copy of it.
Related
This question already has an answer here:
c++ - const member func, that can be called upon lvalue instances only, using a ref-qualifier
(1 answer)
Closed 2 years ago.
For the following code:
class C
{
public:
void fun() const {};
void fun() const & {};
};
I know that this is illegal, since we cannot overload with const and const &. However, my question is: we already have const member function in the old standard, why did we need to introduce the const & member function? Are there any semantic differences between a const member function and a const-ref member function?
What's the difference between const member function and const-ref member function
Lvalue ref qualified function cannot be called on rvalue instance arguments (unless the function is also const qualified). Unqualified member functions can be.
This question already has answers here:
What does the single ampersand after the parameter list of a member function declaration mean?
(3 answers)
What is "rvalue reference for *this"?
(3 answers)
Closed 6 years ago.
Looking at Boost::Optional optional class template header I come across this:
T const& operator*() const&
T& operator*() &;
T&& operator*() &&;
For the life of me I can't find this syntax anywhere else (a reference as the last symbol) I would assume it has something to do with overloading on the type(const l-val, l-val, r-val) of the object the operator belongs to, but I haven't seen this described anywhere.
Could someone tell me what this syntax means?
This question already has answers here:
How come a non-const reference cannot bind to a temporary object?
(11 answers)
Closed 7 years ago.
I just created a cool class in a namespace and therefore I wanted to create 2 functions. They should have the same name, but different parameters.
But every time I compile it, it says:
.....formatter.cpp:25:18: error: no matching member function for call to 'output'
return this->output(DateTime());
~~~~~~^~~~~~
.....formatter.h:57:18: note: candidate function not viable: expects an l-value for 1st argument
const string output(DateTime& time) const;
^
.....formatter.cpp:23:25: note: candidate function not viable: requires 0 arguments, but 1 was provided
const string Formatter::output() const
^
So I assumed that I had done a typo or something, but without success. Because of that I ended up here. Here is the minimized class:
formatter.h
namespace test {
class Formatter
{
public:
const string output() const;
const string output(DateTime& time) const; //DateTime is another class in the same namespace
}
}
formatter.cpp
namespace test {
const string Formatter::output() const
{
return output(DateTime());
}
const string Formatter::output(class DateTime& time) const
{
return "Test";
}
}
Thanks for your help,
~ Markus
And I swear, I have tried any possibility without success...
C++ doesn't allow binding of temporaries (rvalues) to non-const lvalue references, which is what you're doing here:
return output(DateTime());
You can fix this by making the parameter a const reference:
const string output(const DateTime& time) const;
You need
const string output(const DateTime& time) const;
Your calling pattern (DateTime()) creates a temporary. A non-const reference cannot bind to a temporary. You need a const reference.
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 8 years ago.
In the book "Programming Interviews Exposed" by John Mongan (see page 33), theres a function declaration for a Singly Linked List Element:
const T& value() const {...}
I understand what everything before the method name means, namely that it is a reference to a template datatype which you may not modify, but what does the extra const after the value() signify? A constant reference? I thought references were already constant (i.e. unchangeable alias which is the object).
const T& value() const {...}
This means the function value() will return a const T& type and in between (in the function) won't modify the class itself.
Say I write:
class Cfoo
{
void foo() const
{
//Cfoo will not be modified here
}
}
If I directly quote from MS Docs:
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren't constant.
The const after the method name signifies that the method does not modify any field of the object. Therefore, it can be invoked on a const instance of the object.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How come a non-const reference cannot bind to a temporary object?
This program:
int fun()
{
return 1;
}
int main()
{
const int& var = fun();
return 0;
}
My question is why I must put a const befor the var definition? If not ,g++ will give me an error ,goes something like "invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’."
what is the 'const' for ?
In this situation you need const because reference initialization requires a variable with an address, not simply a value. Therefore the compiler must create an anonymous variable which you cannot access other than through the reference; the compiler does not want you to access the variable that you did not declare.
If you would declare the variable explicitly, const would be unnecessary:
int tmp = fun();
int &var(tmp);