Difference between new A and new A

本文深入探讨了C++中POD(Plain Old Data)类型的特点,以及在使用new关键字创建对象时,new A与new A()的区别。通过实例代码展示了不同情况下这两种初始化方式对POD类型和非POD类型的影响。

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

                       

问题:
A是一个类,我们经常new一个A的对象,你经常会这么写:

A *pa = new A();
  
  • 1

但是如果我这么写呢:

A *pa = new A;
  
  • 1

两者有什么区别吗? 这就是我们今天要讨论的问题。

先给出答案,再慢慢解释

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,即一个class或struct没有构造函数、析构函数、虚函数。维基百科上这么描述:

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.
  
  • 1

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.

我们就定义一个POD的类:

class A{  int m;};
  
  • 1
  • 2
  • 3
  • 4

再定义两个非POD类:

class B {   ~B(); }; class C {   C() : m() {};   int m; }; 
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

接下来就是使用了:

#include<iostream>using namespace std;class A{publicint m;};class B{public:  ~B() {};publicint m;};class C{public:  C() : m() {};publicint 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;}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

输出:
18780112
0
4522067
0
0
0

在所有C++版本中,只有当A是POD类型的时候,new A和new A()才会有区别

就是再看看如何判断是不是POD,不用认为判断:

#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';}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

输出:
true
false
false

           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值