When I have a struct
struct Point
{
int x;
int y;
int z;
}
Why it's possible to write
Point p = {10, 20, 30};
Point p0 {10, 20, 30};
auto p1 = Point{10, 20, 30};
Using {}. But I can't write with ()
Point p(10,10,10);
auto p1 = Point(10,10,10);
With error
Error C2440 'initializing': cannot convert from 'initializer list' to 'Point '
Since C++20 you can perform aggregate-initialization with parentheses initializer.
T object (arg1, arg2, ...); (5) (since C++20)
Before C++20, Point p(10,10,10);
is supposed to call a constructor of Point
taking 3 int
s, but Point
doesn't have such constructor. As you've showed you have to use braced initializer to perform aggregate-initialization, or provide such a constructor for Point
then you could use both parentheses and braced initializer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.