c++中struct使用注意事項
更新時間:2016年01月20日 11:29:10 投稿:hebedich
本文通過2個小示例給大家展示了一下c++中struct使用的注意事項,希望對大家學習C++能夠有所幫助。
1.C++的結(jié)構(gòu)體變量在聲明的時候可以省略struct,在c中這樣是不可以的,例子如下
#include<iostream> #include<string> using namespace std; struct test{ int num; string name; }; int main(void) { test t; t.num=1; t.name="jack"; cout<<t.num<<" "<<t.name<<endl; }
2.c++的結(jié)構(gòu)體聲明可以聲明在main()函數(shù)中,也可以在main()函數(shù)之前,在之前的話,整個程序都可以調(diào)用,這也是最常用的方式;而在內(nèi)部的話,只能在函數(shù)內(nèi)使用,也就是說在聲明的函數(shù)內(nèi)可以使用,類似于局部變量的概念。如下
int main(void) { struct test{ int num; }; test hello;//right } void t() { test t; //wrong }
您可能感興趣的文章:
- C++結(jié)構(gòu)體struct和類class區(qū)別詳解
- 淺談C++ Explicit Constructors(顯式構(gòu)造函數(shù))
- C++使struct對象擁有可變大小的數(shù)組(詳解)
- 詳解C++程序中定義struct結(jié)構(gòu)體的方法
- 深入剖析C++中的struct結(jié)構(gòu)體字節(jié)對齊
- C++中聲明類的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解
- C++中關(guān)鍵字Struct和Class的區(qū)別
- C++中typedef 及其與struct的結(jié)合使用
- 淺析c與c++中struct的區(qū)別
- 深入C++中struct與class的區(qū)別分析
- C++ 關(guān)于STL中sort()對struct排序的方法
- c/c++中struct定義、聲明、對齊方式解析
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)之雙向循環(huán)鏈表的實例
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之雙向循環(huán)鏈表的實例的相關(guān)資料,需要的朋友可以參考下2017-06-06將CString字符串輸入轉(zhuǎn)化成整數(shù)的實現(xiàn)方法
下面小編就為大家?guī)硪黄獙String字符串輸入轉(zhuǎn)化成整數(shù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09