Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struct in a struct in C++

I need help to understand well the use of struct

I have this snippet of code:

struct PCD
{
    PointCloud::Ptr cloud;
    std::string f_name;
    PCD() : cloud (new PointCloud) {};
};

But I don't understand how is possible this row:

PCD() : cloud (new PointCloud) {};

or better, what does it do? A struct in the struct?

Where could I find a good explanation of it?

like image 487
SPS Avatar asked Dec 15 '22 05:12

SPS


2 Answers

cloud is a pointer to a PointCloud object. It's a member of the PCD struct. When this struct is initialized using the initializer list, that pointer is allocated a new PointCloud object.

This is likely found in the PointCloud struct/class:

typedef PointCloud* Ptr;
like image 177
Aesthete Avatar answered Dec 27 '22 18:12

Aesthete


PCD() : cloud (new PointCloud) {};

is a PCD constructor that initializes the cloud variable with a new PointCloud instance.

struct PCD
{
    PointCloud::Ptr cloud;
    std::string f_name;
    PCD() : cloud (new PointCloud) {};
};

can be rewritten and visualized as:

struct PCD
{
public:
    PointCloud::Ptr cloud;
    std::string f_name;
    PCD();
};

PCD::PCD() : cloud (new PointCloud) {};
like image 27
Bahl Avatar answered Dec 27 '22 17:12

Bahl