多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > php教程 > Difference between new A and new A()

Difference between new A and new A()

來源:程序員人生   發(fā)布時(shí)間:2016-06-22 08:37:26 閱讀次數(shù):2684次

問題:
A是1個(gè)類,我們常常new1個(gè)A的對(duì)象,你常常會(huì)這么寫:

A *pa = new A();

但是如果我這么寫呢:

A *pa = new A;

二者有甚么區(qū)分嗎? 這就是我們今天要討論的問題。

先給出答案,再漸漸解釋

If the class has a default constructor defined, then both are equivalent; the object will be created by calling that constructor.

If the class only has an implicit default constructor, then there is a difference. The first will leave any members of POD type uninitialised; the second will value-initialise them .

甚么是POD

POD的意思是 Plain Old Data,即1個(gè)class或struct沒有構(gòu)造函數(shù)、析構(gòu)函數(shù)、虛函數(shù)。維基百科上這么描寫:

A Plain Old Data Structure in C++ is an aggregate class that contains only PODS as members, has no user-defined destructor, no user-defined copy assignment operator, and no nonstatic members of pointer-to-member type.

int, char, wchar_t, bool, float, double are PODs, as are long/short and signed/unsigned versions of them.

pointers (including pointer-to-function and pointer-to-member) are PODs,
enums are PODs

a const or volatile POD is a POD.

a class, struct or union of PODs is a POD provided that all non-static data members are public, and it has no base class and no constructors, destructors, or virtual methods. Static members don’t stop something being a POD under this rule.

我們就定義1個(gè)POD的類:

class A { int m; };

再定義兩個(gè)非POD類:

class B { ~B(); }; class C { C() : m() {}; int m; };

接下來就是使用了:

#include<iostream> using namespace std; class A { public: int m; }; class B { public: ~B() {}; public: int m; }; class C { public: C() : m() {}; public: int m; }; int main() { A *aObj1 = new A; A *aObj2 = new A(); cout << aObj1->m << endl; cout << aObj2->m << endl; B *bObj1 = new B; B *bObj2 = new B(); cout << bObj1->m << endl; cout << bObj2->m << endl; C *cObj1 = new C; C *cObj2 = new C(); cout << cObj1->m << endl; cout << cObj2->m << endl; delete aObj1; delete aObj2; delete bObj1; delete bObj2; delete cObj1; delete cObj2; return 0; }

輸出:
18780112
0
4522067
0
0
0

在所有C++版本中,只有當(dāng)A是POD類型的時(shí)候,new A和new A()才會(huì)有區(qū)分

就是再看看如何判斷是否是POD,不用認(rèn)為判斷:

#include <iostream> #include <type_traits> struct A { int m; }; struct B { int m1; private: int m2; }; struct C { virtual void foo(); }; int main() { std::cout << std::boolalpha; std::cout << std::is_pod<A>::value << '\n'; std::cout << std::is_pod<B>::value << '\n'; std::cout << std::is_pod<C>::value << '\n'; }

輸出:
true
false
false

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 亚洲第一精品夜夜躁人人爽 | 欧美xxxx性猛交bbbb | 日本www在线播放 | 亚洲产国偷v产偷v自拍涩爱 | 欧美一区二区视频在线观看 | 在线观看91精品国产性色 | 一二三中文乱码亚洲乱码 | 亚洲欧美日韩中文字幕在线一区 | 老司机免费福利在线观看 | 久久精品综合 | 亚洲线精品久久一区二区三区 | 国产亚洲精 | 欧美日韩一区二区高清视 | 欧美xxxx做受欧美护士 | 激情爽毛片私人影院 | 亚洲欧美中文字幕高清在线一 | 高清完整视频在线播放 | 色悠久久久久综合欧美99 | 欧美综合精品一区二区三区 | 中国在线播放精品区 | 色亚洲影院 | 意大利xxx | 欧美色综合天天综合高清网 | 欧美国产亚洲一区二区三区 | 亚洲欧美日韩中文字幕一区二区三区 | 国产女人18一级毛片视频 | 精品国产91久久久久久久 | 亚洲国产天堂久久综合2261144 | 国产成人精品午夜在线播放 | 亚洲日本1区2区3区二区 | 亚洲欧美专区精品久久 | 伊人福利网| 午夜dj免费视频观看在线播放 | 在线观看www | h在线观看免费 | 久久99毛片免费观看不卡 | 在线黄视频网站 | 涩涩视频www在线观看入口 | 欧美精品18videos性欧美 | 色综合美国色农夫网 | 国产农村精品一级毛片视频 |