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
Related
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.
This question already has answers here:
How do I declare a 2d array in C++ using new?
(29 answers)
Expression does not evaluate to a constant
(1 answer)
Closed 7 months ago.
const int32_t kernel_sizes[1] = {8};
void Create(int id)
{
float *kernel = new float[1][1][kernel_sizes[id]];
}
Create(0);
When I try to create this array, there is a error. The error is:
Array size is not a constant expression implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function. What is the reason for this error and how to solve this error?
This question already has answers here:
Pointer to class data member "::*"
(18 answers)
Closed 7 years ago.
I cannot figure out how this compiles. It seems like it should not, and if I use a value other than NULL in the constructor it will not.
#include <stdio.h>
class MyClass{
private:
int *first;
public:
MyClass();
};
MyClass::MyClass(){
int whatever = 42;
//int* MyClass::*first = &whatever;//This does not compile
int* MyClass::*first = NULL;//This compiles
}
int main(){
MyClass doSomething;
return 1;
}
It seems that generally the type Class::member = value syntax is used for static vars, which this is not.
Also, there is an asterisk before the member name, which confuses things even more.
If I switch the lines to the one that is commented out, the compiler complains, as expected.
error: cannot convert ‘int*’ to ‘int* MyClass::*’ in initialization
While I did expect an error, I have no idea what the type int* MyClass::* is. Or how it could be used.
This is a pointer to data member. You cannot initialize it with an ordinary pointer, that is why the commented out expression does not compile.
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 following two lines ?
int *a = new int;
int *a = new int();
int *a = new int;
a is pointing to default-initialized object (which is uninitialized object in this case i.e the value is indeterminate as per the Standard).
int *a = new int();
a is pointing to value-initialized object (which is zero-initialized object in this case i.e the value is zero as per the Standard).
The first variant default-initializes the dynamically allocated int, which for built-in types such as int does not perform any initialization.
The second variant value-initializes it, which for int means zero-initialization, giving it value 0.
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.