C++ 结构体

C++ 结构体,第1张

1、结构体定义

用结构体定于如下图位置

#include
using  namespace std;
struct RECT {
	int top{ 1 };
	int bottom{ 1 };
	int left{ 1 };
	int right{ 1 };
};
 void  MoveRECT (RECT &aRect,int x, int y) //直接引用实参进行位置移动
 {
	 aRect.top = aRect.top + y;
	 aRect.bottom = aRect.bottom + y;
	aRect.left= aRect.left+x;
	aRect.right = aRect.right + x;
};

int main()
{

	RECT yard{ 0,0,100,120 };
	RECT pool{ 30,40,70,80 };
	RECT hut1, hut2;
	hut1.top = 10;
	hut1.bottom = 30;
	hut1.left = 70;
	hut1.right = hut1.left + 25;
	hut2 = hut1;
	MoveRECT(hut2, -60, 80); 
	cout << hut2.left;
	return 0;
}

2. 指针访问结构体成员

包括直接访问和间接方案

#include
using  namespace std;
struct RECT {
	int top{ 1 };
	int bottom{ 1 };
};

int main()
{

	RECT aRect{ 3, 4 }; //结构体对象初始化
	RECT* PaRect;
	PaRect = &aRect;
	cout << (*PaRect).top; //访问结构体成员
	cout << PaRect->bottom;//间访结构体成员
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562255.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-01
下一篇 2022-04-01

发表评论

登录后才能评论

评论列表(0条)

保存