结构体构造函数
【知识点:结构体构造函数】
下面两段代码等价。
(1)结构体构造函数写法
struct LinkNode {int data;LinkNode* next;LinkNode(int x):data(x),next(NULL) {}
};
LinkNode* L=new LinkNode(123);
(2)非结构体构造函数写法
struct LinkNode {int data;LinkNode* next;
};
LinkNode* L=new LinkNode;
L->data=123;
L->next=NULL;
【算法实例】
#include <bits/stdc++.h>
using namespace std;struct Point {int x,y;Point() {} //无参构造Point(int _x,int _y):x(_x),y(_y) {} //有参构造
} pt[10];int main() {int cnt=0;for(int i=1; i<=3; i++) {for(int j=1; j<=3; j++) {pt[cnt++]=Point(i,j);}}for(int i=0; i<cnt; i++) {printf("%d %d\n",pt[i].x,pt[i].y);}return 0;
}/*
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
*/
【参考文献】
https://www.cnblogs.com/wlw-x/p/11566191.html
https://blog.csdn.net/icecreamTong/article/details/130627646
https://blog.csdn.net/qq_35812205/article/details/112801861