[英]struct in a struct in C++
我需要幫助才能很好地理解struct
的用法
我有這段代碼:
struct PCD
{
PointCloud::Ptr cloud;
std::string f_name;
PCD() : cloud (new PointCloud) {};
};
但我不明白這行怎么可能:
PCD() : cloud (new PointCloud) {};
或者更好,它做什么? struct
中的struct
?
我在哪里可以找到一個很好的解釋?
cloud
是指向PointCloud
對象的指針。 它是PCD
結構的成員。 當使用初始化列表初始化此結構時,該指針被分配一個新的PointCloud
對象。
這可能在PointCloud
結構/類中找到:
typedef PointCloud* Ptr;
PCD() : cloud (new PointCloud) {};
是一個PCD構造函數,它使用新的PointCloud實例初始化雲變量。
struct PCD
{
PointCloud::Ptr cloud;
std::string f_name;
PCD() : cloud (new PointCloud) {};
};
可以重寫和可視化為:
struct PCD
{
public:
PointCloud::Ptr cloud;
std::string f_name;
PCD();
};
PCD::PCD() : cloud (new PointCloud) {};
結構是一種一切都公開的階級。 在這里,您正在查看struct PCD的默認構造函數以及其數據成員之一的初始化。 我們不知道PointCloud是結構還是類,但似乎PCD在該類型的實例上包含指針。 所以默認構造函數創建一個新實例。
PCD() : cloud (new PointCloud) {};
這是結構PCD
的默認構造函數。
:
語法表示正在使用成員初始值設定項列表來初始化一個或多個結構數據成員。 在這種情況下,指針cloud
被分配一個新的,動態分配的PointCloud
對象。
成員初始值設定項列表用於在執行構造函數體之前初始化非靜態數據成員。 它也是初始化參考類型成員的唯一方法。
有關構造函數和成員初始化列表的詳細信息在這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.