【发布时间】:2021-10-23 02:51:48
【问题描述】:
我已经创建了一个类Point,这里是对应的hpp文件。
#ifndef POINT
#define POINT
class Point
{
protected:
int x;
int y;
public:
Point(int x = 10, int y = 10);
void movePoint(int moveX, int moveY);
void printCoordinates();
};
#endif
我注意到,在主体中,我可以这样声明一个对象并对其进行初始化:
Point myPoint(1, 1);
如果我想创建一个包含两个点的结构,它不会让我这样初始化它,而是必须使用大括号,这样:
struct segment
{
Point point1 = {0, 0};
Point point2 = {15, 15};
};
这是为什么呢?
【问题讨论】:
-
想象一下要求编译器在
template<typename B> class S : public B中解析int x(n);,其中不知道n是int,还是来自B的类型名
标签: c++ c++11 struct initialization member-initialization