结构体是否有默认的构造函数?(没有,如果需要,需要自己写)
生活随笔
收集整理的這篇文章主要介紹了
结构体是否有默认的构造函数?(没有,如果需要,需要自己写)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// node type used by winner tree#ifndef player_
#define player_struct player //競賽樹的每個外節點
{int id, key; //id表示元素所在的順串號,key表示元素的值 operator int () const {return key;} //有了這個函數就可以像內建數據類型一樣把player類型 強制/自動轉換成int型了。
};#endif
在這里插入代碼片
#include <iostream> #include "player.h" using namespace std; int main() {player a;a.id=1;a.key=2; // player b=a;// player b(a);//player b(1,2);cout<<b.id<<endl;cout<<b.key<<endl; }*上面程序運行后, 發現player b=a;以及player b(a);都可以,但player b(1,2)會報錯??梢?#xff0c; 結構體不能默認生成構造函數, 但是結構體對象相互之間的賦值還是可以的。*總結
以上是生活随笔為你收集整理的结构体是否有默认的构造函数?(没有,如果需要,需要自己写)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在 word 中 mathType 菜单
- 下一篇: 有关scanf输入的问题