Why references can not be reinitialized in C++? [duplicate] - c++

This question already has answers here:
Why are references not reseatable in C++
(17 answers)
Closed 9 years ago.
Why references can not be reinitialized in C++ while pointers can be reinitialized?
int x=5;
int y=6;
int *p1;
p1 = &x;
p1 = &y; //re-initializing the pointer but same can not be done with references
int &r1 =x;//can be initialized only once

There's no obvious syntax. You can't use the normal = syntax; that sets the value the underlying pointer of the reference. Perhaps you could think up a syntax like this:
&my_reference = new_value;
But that's kind of strange and awkward.

Related

Is it UB to modify a const object's member via its non-const reference? [duplicate]

This question already has answers here:
C++ const object's member can be modified
(1 answer)
Why does C++ not have a const constructor?
(5 answers)
C++, Classes, Const, and strange syntax
(8 answers)
Closed 3 months ago.
class T {
public:
int v;
int &vRef;
public:
T() : v(0), vRef(v) {}
};
const T t; // note, it's a const object
t.vRef = 2;
printf("v: %d\n", t.v);
The code presented above compiles OK, and the const object's internal value did change.
Question. Is this Undefined Behavior or not?
Yes. If the object is declared as const, then modifying it (through any means, be that a non-const reference like in your example, via const_cast or something else) is UB.

Assigning C++ pointers [duplicate]

This question already has answers here:
Constant pointer vs Pointer to constant [duplicate]
(8 answers)
Closed 2 years ago.
I am new to C++ and just started learning pointers. If I write the code -
const int n = 4;
int m = 4;
const int *p = &n;
I understood this much that const makes the variable unchangeable. So if I want to change n, I cannot, but what value is the pointer having that is unchangeable? Because the next code is executing propery -
p = &m;
Shouldn't this give an error since it was already storing the value of n in the first place? I am sorry if it is a dumb question.
const int *p = &n;
The pointer above is not unchangeable, what it is pointing to is unchangeable
const int * const cp = &n;
cp = &m; // error here
This pointer is unchangeable. To make a pointer unchangeable put const after the *.
It seems the most important lesson about pointers (and one that I see beginners get confused about all the time) is the the pointer, and what it is pointing at are two separate things. In this case either the pointer or what it is pointing at can be const (or both can be).

is_const<const int&>::value is false -- why? [duplicate]

This question already has answers here:
Why does std::is_const<const int&>::value evaluate to false?
(2 answers)
Closed 7 years ago.
Why does this static assertion fire?
static_assert(std::is_const<const int&>::value, "Pain");
Would be awesome to get both a syntactic (why the implementation would do this) and a semantic reasoning (why they would have designed this type trait's interface to do this).
I am aware it is possible to throw in a std::remove_reference call to get the expected outcome, but I'm not sure why that's necessary.
const int& is a reference to const int. So the reference itself isn't const.
It's slightly confusing, so I'm going to present an analogy with const int*. It's the pointer to const int. But you can modify it
const int a = 5, b = 7;
const int* ptr = &a;
ptr = &b; // pointer is modified
so the pointer isn't const. (the const pointer would be int* const instead)

What is the Difference between Constant and Volatile in C [duplicate]

This question already has answers here:
What are the differences between const and volatile pointer in C?
(4 answers)
Usage wise difference between const & volatile qualifier in C?
(6 answers)
Closed 9 years ago.
what is the difference between Constant and Volatile
and
What is difference
Volatile int i =10
And
int i =10;
please let me briefly
For a start
int i = 0;
Is not a constant, as it requires the const keyword. A constant cannot be modified and requires to be initialized (you can't put const int i; i = 10;)
Volatile [variables] can be modified externally, for example
asm volatile
Would state the the assembly code can be externally modified

C++ class initialization with and without parentheses [duplicate]

This question already has answers here:
Do the parentheses after the type name make a difference with new?
(7 answers)
Closed 9 years ago.
What is the difference between the following 2 two initializations?
class Pod {
public:
int a, b;
};
Pod *p1 = new Pod;
Pod *p2 = new Pod();
In the first case the object is left uninitialized, while in the second case the object is guaranteed to be value-initialized, which in this case as the type is POD it means zero-initialized