c++中 class与struct之间的区别

c++中 class与struct之间的区别

c++中保留了c语言的struct关键字,并赋予其新功能。在c中,struct只能包含成员变量,没有成员函数,而在cpp中,可以同时包含二者。在c++中而这基本通用,细节红色那个有所不同。

  1. 使用class时,类众多成员默认是private属性的,而使用struct时,机构体中的成员默认时public属性的。
  2. class继承默认是private继承,而struct默认继承是public继承。
  3. class可以使用模板,而struct不可以。
  4. 结构体不能只写有参构造函数,如果只写了有参构造将会出现无法初始化的错误。

实例代码


struct GridNode
{
	int id;
	// 1--> open set, -1 --> closed set
	Eigen::Vector3d coord;
	Eigen::Vector3i dir; // direction of expanding, only for JPS
	Eigen::Vector3i index;
	double gScore, fScore;
	GridNodePtr cameFrom;
	std::multimap<double, GridNodePtr>::iterator nodeMapIt;
	GridNode(Eigen::Vector3i _index, Eigen::Vector3d _coord){
	id = 0;
	index = _index;
	coord = _coord;
	dir = Eigen::Vector3i::Zero();
	gScore = inf;
	fScore = inf;
	cameFrom = NULL;
	}
	GridNode(){};
	~GridNode(){};
};

定义节点的结构体,有参构造并不单独出现。

参考

http://c.biancheng.net/view/2235.html
https://www.cnblogs.com/wlw-x/p/11566191.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值