这个函数"a::b::ptr function(value)"调用在 C++ 中是如何工作的?



我实际上是c++的新手,我正试图弄清楚编译器如何执行下面的行:

pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());

我知道创建了一个类型为pcl::ModelCoefficents()的堆内存,并将其指针传递给函数coefficients()。让我困惑的是,我们不应该使用下面这样的箭头运算符吗:

pcl::ModelCoefficients::Ptr->coefficients (new pcl::ModelCoefficients ());

语句

pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());

可以重写为

pcl::ModelCoefficients::Ptr coefficients = new pcl::ModelCoefficients;

我认为第二个版本更好地显示了正在发生的事情。

简而言之,该行定义了一个变量,名为coefficients,类型为pcl::ModelCoefficients::Ptr。然后用new pcl::ModelCoefficients的结果初始化coefficients

最新更新