簡體   English   中英

在C ++中的struct中的struct

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM