gpt4 book ai didi

c++ - 为什么结构内部的对象初始化不同?

转载 作者:行者123 更新时间:2023-12-02 16:07:57 24 4
gpt4 key购买 nike

<分区>

我创建了一个类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

我注意到在 main 中,我可以声明一个对象并以这种方式初始化它:

Point myPoint(1, 1);

如果我想创建一个包含两个点的结构,它不会让我以这种方式初始化它,相反,我必须使用大括号,这样:

struct segment
{
Point point1 = {0, 0};
Point point2 = {15, 15};
};

这是为什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com