Questions tagged [const-reference]
131 questions
0
votes
1 answer
decltype and parenthesis answers are wrong?
I read this : decltype and parentheses
But I can't understand the answers !
If the type of (a->x) is const double& why does this code run ?!
#include
struct A { double x; };
int main()
{
A *a=new A;
decltype(a->x) x3;
…

uchar
- 2,552
- 4
- 29
- 50
0
votes
5 answers
defining NULL/Empty value for standard C++ types
My class has methods that return const & because I don't want the returned container to be modified outside and copying could be expensive. for Example: const std::set& foo() and I want foo() to be able to return a const reference…

Chenna V
- 10,185
- 11
- 77
- 104
0
votes
2 answers
new-expression and delete-expression on const reference and const pointer
C++
Much literature says const references cannot be used to modify their referents and const pointers cannot be used to modify their pointees.
Then, why can they be deleted?
const int& cirDynamic = *( new int(5) );
// ^ 'const int& cirDynamic = *(…

CodeBricks
- 1,771
- 3
- 17
- 37
0
votes
1 answer
Particular form of bind temporary to const reference has no effect
Given
struct A
{
void a(void) { std::cout << "A" << std::endl; }
};
const A &a = A(); /* Make a copy of A and bind to a */
const A &b(A()); /* Does nothing */
a.a(); /* Prints A */
b.a(); /* Error, same as if b doesn't exist */
Why does the…

DOS
- 93
- 1
- 6
0
votes
1 answer
Why a temp value cannot be used in a constructor with a const reference argument?
The code is as below
class A {};
class B
{
public:
B(const A& a) {}
void fun() {}
};
int main(int argc, char *argv[])
{
B b(A());
b.fun(); // Error: left of '.fun' must have class/struct/union
A a;
B b2(a);
b2.fun();…

user1899020
- 13,167
- 21
- 79
- 154
0
votes
3 answers
Why a reference is likely as an object?
Currently learning c++ and nowhere else better to ask something than to the experts of S.O. I Couldn't find more complete and better answers than here. So there it goes.
DWORD dw = 5;
cout << &dw;
Displays the address where the value of dw is…

Vinícius
- 15,498
- 3
- 29
- 53
-1
votes
3 answers
Where a const reference refers to?
There is a simple program
#include
int counter = 3;
const int& f() {
return counter++;
}
const int& g() {
return counter++;
}
const int& h(int w) {
counter = w;
return counter;
}
void main()
{
const int& x = f();
…

Dmitry
- 116
- 4
-2
votes
1 answer
Why does top()'s return value change after calling pop()?
The const reference returned by priority_queue's top() changes after calling pop() (visual studio 2015)
priority_queue queue;
queue.push(1);
queue.push(2);
queue.push(3);
const int & m = queue.top();
cout << m << endl; // 3
queue.pop();
cout…

stanleyerror
- 728
- 1
- 9
- 23
-3
votes
1 answer
C++, const reference is actually faster than move?
After testing this code:
#include
#include
#include
#include
void x(std::vector&& v){ }
void y(const std::vector& v) { }
int main() {
std::vector v = {};
auto…

Levon
- 250
- 2
- 12
-4
votes
2 answers
How to call object T from constructor copy T(const T&)?
I have a copy constructor T::T(const T&). The object has two properties, let's say color and height. This means I need to assign the color and the height from the object in argument to my object. Problem is I don't know how to call the argument…

Marco E
- 93
- 1
- 6
-4
votes
1 answer
How to access the value of a const reference parameter?
if I have a function set up similarly to this:
update_value(const int& old_value, const int& new_value){
}
What if I need to get the value of old_value and new_value and use these values in a mutable variable?
I can't dereference with a *pointer.…

Ben361
- 1