简体   繁体   中英

Writing struct inside a class

this is a general question about C++. I'm trying to make a Camera class which will contain private member struct containing various camera properties.

Class Camere
{
public:


int read_input_image(properties &c, const char *file_name);      

private:
struct properties
{
double* input_image;
double* output_image;
----
----
}
}

properties c; }

.cpp file

int Camera :: read_input_image(properties &c, const char *file_name) {
        if(c.input_image == 0) {
        c.input_image = new uint8_image_t;
        c.input_image->data = 0;
    }

Now, 1. My question is how would I get access to private members of struct, if I want to write a function accessing the struct members. 2. Another question is how should I delete the pointers associated with struct inside class, do I need to delete input_image pointer.

how would I get access to private members of struct

The struct has no private members, all of its member are public because the default access for structs is public.

do I need to delete input_image pointer

Yes, you need to delete input_image because it points to new ed memory.

It's not the members of your struct that are private, but the the definition of the struct itself.

As the members are not private (members of structs are public unless specified otherwise), you can access them at any point where struct properties is known, in this case only inside class Camere . Outside of class Camere , struct properties is not known and cannot be accessed. Note that this is not because any "private" members (which are not private) but due to the fact that the definition of struct properties is not exposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM