Questions tagged [const-reference]
131 questions
0
votes
2 answers
How to initialize a const reference member to another member (std vector) in C++ initializer list
I did the following as a cheap way to allow read-only access to a member container _numbers via numbers:
class Foo {
Foo() : _numbers({}), numbers(_numbers) {
// some code that populates `numbers` via `numbers.push_back(...)`
}
private:
…

flonk
- 3,726
- 3
- 24
- 37
0
votes
1 answer
C++ const reference template function argument type is itself a reference, why?
I have the following function:
template
void f(const T& val) {
using value_type = T;
using sub_type = typename value_type::sub_type;
//etc...
}
However, I am running into the problem that the compiler is telling me that T is…

111111
- 15,686
- 6
- 47
- 62
0
votes
1 answer
Prefered way to pass a const reference/pointer to a class for storing, not to copy the referred object
Example:
class Bar;
class Foo
{
public:
Foo(const Bar& bar) : mBar(&bar) {}
/* Other methods use mBar. */
private:
const Bar* mBar;
};
So the goal is to store a const pointer to some external object, not to store a copy of the…

Unimportant
- 2,076
- 14
- 19
0
votes
0 answers
Why PVS-Studio states that there is a non-constant reference?
I must first admit that I have very little experience with C++, so please forgive me if the question looks somewhat stupid.
I've found a puzzling element while analyzing project with PVS-Studio. Here's the code to reproduce the question:
class…

Cerberus
- 8,879
- 1
- 25
- 40
0
votes
1 answer
const ref binding to internal resource of rvalue
Consider the code:
const Resource& r = ResourceContainer("foo").myResource;
What does the standard say about the lifetime of myResource?
Similar, but not sure if this is equivalent: now imagine that it was being implicitly converted via operator…

Patrick Parker
- 4,863
- 4
- 19
- 51
0
votes
0 answers
Const reference as a member of a class insconsistency
If one creates a const reference to a temporary, its life is extended as if the reference where in the stack.
It is a good feature of the language, although it is presented sometimes like an exception to other rules.…

alfC
- 14,261
- 4
- 67
- 118
0
votes
1 answer
Passing a const reference as a function parameter in a class member function
Suppose we have a class called Line that contains the following modifier that assigns a value to a class data member.
void Start(const Point &start);
The line class contains two data members called start and end which are both Point objects. Since…

Mutating Algorithm
- 2,604
- 2
- 29
- 66
0
votes
1 answer
using friend function with const reference in operator overloading
The code below cannot be compiled. However, when I remove "const" from Point& of the friend function, this code turns to be compiled. Could anyone explain the reason why?
class Point
{
public:
Point(double x, double y);
Point…

orematasaburo
- 1,207
- 10
- 20
0
votes
1 answer
C++ storing a const& to a temporary
Recently I have run into a problem in a project. The code there is much more complicated than the following example but I hope the problem (if there is one) is the same.
#include
class mObject
{
public:
mObject(){ std::cout <<…

mecahi
- 109
- 2
0
votes
1 answer
C++ Declare const variable, but postpone its initialisation?
Context:
A function (from some API I cannot modify) returns a constant reference to an object from an objectRegistry:
const Foo& f(args)
I need to obtain the constant Foo object, but I require a different instance of Foo based on some…

Kevin van As
- 35
- 8
0
votes
1 answer
Is using rvalue reference to erased element of STL container undefined behavior?
Part of my program does something like this:
#include
#include
std::unordered_map G{{"1", 10}};
int m(const std::string& i, std::unordered_set& v) {
v.erase(i);
return…

YiFei
- 1,752
- 1
- 18
- 33
0
votes
1 answer
Defined function returning const reference to class member and copy of the variable
I am still little bit confused by returning a const reference. Probably, this has been already discussed, however let's have following code as I did not find the same:
#include
#include
struct A
{
int dataSize;
…

Dom
- 532
- 1
- 9
- 23
0
votes
0 answers
String const reference in constructor vs a int const reference
I am trying to learn by experimenting with constructors and references. I wrote the class as follows and I expect the answer written after the class
#include
#include
class human {
public:
const std::string& m_name;
void…

Arul Moondra
- 131
- 1
- 9
0
votes
1 answer
const_reference or iterator for map (when not actually iterating)
I have some code that uses an iterator to loop through all elements of an unordered_map, but within that loop there are several other processes where I store iterators to particular elements in the map, and generally do lots of jumping around but…

zenna
- 9,006
- 12
- 73
- 101
0
votes
2 answers
Is using a reference parameter with default value good practice?
I have the following code:
#include
#include
void f(const std::string& s = "")
{
std::cout << "\"" << s << "\"" << std::endl;
}
int main()
{
std::string s1 = "qwe";
f();
f("asd");
f(s1);
}
How bad (if at all) are the…

Patryk
- 22,602
- 44
- 128
- 244