用C ++初始化对象的不同方法 [英] Different ways of initializing an object in c++

查看:63
本文介绍了用C ++初始化对象的不同方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下此类:

 类实体{
public:
int x,y;

Entity():x(0),y(0){}
Entity(int x,int y):x(x),y​​(y){}
}

以下是用我想知道的东西初始化类的多种方法:

 实体ent1; //使用默认构造函数,因此x = 0和y = 0 
Entity ent2(); //使用默认构造函数,因此x = 0和y = 0(不确定)
Entity ent3(1,2); //构造函数,因此x = 1和y = 2
Entity ent4 = Entity(); //默认构造函数,因此x = 0和y = 0
Entity ent5 = Entity(2,3); //构造函数,因此x = 2和y = 3

我知道可以在堆上创建对象内存,但这不是我现在要寻找的。

我的问题是,初始化对象的这些方式之间有什么区别?


解决方案

  Entity ent1; 

此语句使用类 Entity 的默认构造函数

 实体ent2();。 

如果可能,此定义将被编译器视为函数原型。

 实体ent3(1,2); 

ent3 调用用户定义的构造函数

 实体ent4 = Entity();。 

这是 适当 版本的 ent2 的情况。默认构造函数作为值初始化的一部分被调用。它的形式可以避免最令人讨厌的解析-导致 ent2 不正确的歧义解决原理。

 实体ent5 = Entity(2,3); 

ent3案例的一种版本。用户定义的构造函数作为值初始化的一部分被调用。



您的问题被标记为C ++ 11,并且C ++ 11允许统一的初始化语法:

 实体ent12 {}; //这是ent2案例
的合法替代品实体ent13 {1,2};
实体ent14 = {};
实体ent15 =实体{2,3};

请注意,统一初始化语法有一些警告。例如。这行

  std :: vector< int> v(10); 

声明10个元素的向量。但这

  std :: vector< int> v {10}; 

声明一个向量,该向量用类型为int的int类型的单个元素初始化,其值是10。 c> std :: vector 具有定义以下签名的构造函数:

  vector(std :: initializer_list< T> init,const Allocator& alloc = Allocator()); 

如果您不使用()而不触发MVP或{}而不使用不希望的构造函数,则值初始化赋值语法可以解决该问题。


Imagine this class:

class Entity {
public:
    int x, y;
    
    Entity() : x(0), y(0) { }
    Entity(int x, int y) : x(x), y(y) { }
}

And here are multiple ways of initializing the class with what I think I know:

Entity ent1;                //Uses the default constructor, so x=0 and y=0
Entity ent2();              //Uses the default constructor, so x=0 and y=0 (Not sure)
Entity ent3(1, 2);          //Made constructor, so x=1 and y=2
Entity ent4 = Entity();     //Default constructor, so x=0 and y=0
Entity ent5 = Entity(2, 3); //Made constructor, so x=2 and y=3

I know that's it's possible to make an object on the heap memory, but that's not what I am looking for at this moment.

My question is, what's the difference between these ways of initializing an object?

I'm not sure which one I should use when.

解决方案

Entity ent1;

This statement uses default constructor of class Entity.

Entity ent2();

This definition will be treated by compiler as a function prototype if that's possible.

Entity ent3(1, 2);

User-defined constructor is invoked for ent3.

Entity ent4 = Entity();

This is a proper version of ent2 case. Default constructor is invoked as part of value initialization. Its form allows to avoid Most Vexing Parse - ambiguity solving principle which makes ent2 incorrect.

Entity ent5 = Entity(2, 3);

A version of ent3 case. User-defined constructor invoked as part of value initialization.

Your question is tagged as C++11, and C++11 allows uniform initialization syntax:

Entity ent12{};     // This is a legal alternative of ent2 case
Entity ent13{1, 2};
Entity ent14 = {};
Entity ent15 = Entity{2, 3};

Note that uniform initialization syntax has a caveat. E.g. this line

std::vector<int> v(10); 

declares a vector of 10 elements. But this one

std::vector<int> v{10};

declares a vector initialized with single element of type int with value 10. This happens because std::vector has a constructor with following signature defined:

vector( std::initializer_list<T> init, const Allocator& alloc = Allocator() );

In case that you can't use neither () without triggering MVP nor {} without invoking undesired constructor, the value initialization assignment syntax allows to resolve the issue.

这篇关于用C ++初始化对象的不同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆