本文介绍了C ++用花括号声明对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中声明对象时-





<$有什么区别p $ p> MyClass myObj(a,b,c);

(我理解这是调用带有三个参数的构造函数)



vs。

  MyClass myObbj {a,b,c}; 

不确定此处的大括号是什么意思吗?



我正在使用以下代码作为参考

  //惯性导航EKF 
NavEKF EKF {&小时,气压计,声纳};
AP_AHRS_NavEKF ahrs {ins,气压计,gps,声纳,EKF};

1。
是在
NavEKF EKF中使用花括号吗?{& ahrs,气压计,声纳}; c ++标准的一部分。 gcc 4.6.1抱怨-函数定义未声明参数。



2。
赋予
AP_AHRS_NavEKF ahrs;



AP_Baro气压计;





2A。编译器为何会抱怨



NavEKF EKF(& ahrs,气压计,声纳);



但是允许这样做



NavEKF EKF(AP_AHRS_NavEKF& ahrs,AP_Baro气压计,RangeFinder声纳);

解决方案

基本意图是它们通常表示相同的意思。最大的区别是花括号消除了试图定义对象的最烦人的解析,但最终实际上是声明了一个函数。



因此, NavEKF EKF {& ahrs,晴雨表,声纳}; 通常 等效于 NavEKF EKF(& ahrs,气压计,声纳); 。但是,如果您有类似以下内容: NavEKF EKF(); NavEKF EKF {}; - NavEKF EKF(); 声明一个名为EKF的函数,该函数不带任何参数并返回 NavEKF ,而 NavEKF EKF {}; 会(可能)执行您通常期望的操作,并创建一个名为 EKF 的默认初始化对象键入 NavEKF



在某些情况下,如何解释事物存在歧义,而歧义是根据您使用括号还是花括号,解析方式有所不同。特别是,如果您使用花括号,并且有任何方法可以将内容解释为initializer_list,那么它将被解释。当且仅当失败,因此不能将其视为initializer_list时,它将查找将花括号中的元素作为参数的重载(此时,您将获得正常的重载分辨率,因此它基于最合适的方法,而不仅仅是是否可以解释为完全合适?


When declaring objects in c++ -

What is the difference between

MyClass myObj (a,b,c);

(I understand this as invoking the constructor that takes three arguments)

vs.

MyClass myObbj{a,b,c};

Not sure what the curly brackets here mean?

As a reference I am using the following code

// Inertial Navigation EKF
NavEKF EKF{&ahrs, barometer, sonar};
AP_AHRS_NavEKF ahrs{ins, barometer, gps, sonar, EKF};

1.Is the use of curly brace as inNavEKF EKF{&ahrs, barometer, sonar}; part of c++ standard. gcc 4.6.1 complains that - Function definition does not declare parameters.

2.GivenAP_AHRS_NavEKF ahrs;

AP_Baro barometer;

RangeFinder sonar;

2A. Why would the compiler complain about this

NavEKF EKF(&ahrs, barometer, sonar);

but allow this

NavEKF EKF(AP_AHRS_NavEKF &ahrs, AP_Baro barometer, RangeFinder sonar);

解决方案

The basic intent is that they usually mean the same thing. The big difference is that curly braces eliminate the "most vexing parse" where you tried to define an object, but end up actually declaring a function instead.

So, NavEKF EKF{&ahrs, barometer, sonar}; is usually going to be equivalent to NavEKF EKF(&ahrs, barometer, sonar);. However, if you had something like: NavEKF EKF(); vs. NavEKF EKF{}; they're different--NavEKF EKF(); declares a function named EKF that takes no parameters and returns a NavEKF, whereas NavEKF EKF{}; does what you'd (probably) usually expect, and creates a default-initialized object named EKF of type NavEKF.

There are cases, however, where there's ambiguity about how to interpret things, and the ambiguity is resolved differently depending on whether you use parentheses or braces. In particular, if you use curly braces, and there's any way to interpret the contents as an initializer_list, that's how it'll be interpreted. If and only if that fails, so it can't be treated as an initializer_list, it'll look for an overload that takes the elements in the braces as parameters (and at this point, you get normal overload resolution, so it's based on the best fit, not just "can it possibly be interpreted to fit at all?")

这篇关于C ++用花括号声明对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 16:57