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

How does this function "a::b::ptr function(value)" call work in 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