1.typedef与操作符重载
struct Pseat{ int x; int y; bool operator==(Pseat& rhs) { return x==rhs.x&&y==rhs.y; } //操作符重载 };
写成typedef struct Pseat{...};也是正确的。
但如果写成下面的代码会报错:
typedef struct { int x; int y; bool operator==(Pseat& rhs) { return x==rhs.x&&y==rhs.y; } //操作符重载 }Pseat;
2.类与结构体对象都是可以直接返回的
Pseat findNextSeat(Pseat cur,int di) //结构体是可以直接返回的? { Pseat nextSeat; if(di==1) { nextSeat.x=cur.x+1; nextSeat.y=cur.y; } return nextSeat; }
该代码是正确的。
本文探讨了C++中结构体的使用及操作符重载的方法,并通过示例代码展示了如何定义结构体以及如何实现两个结构体实例间的等价比较。此外,文章还讨论了结构体作为函数返回值的正确性和可行性。
1355

被折叠的 条评论
为什么被折叠?



