Angelina
Angelina

Reputation: 7

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.

Upvotes: 0

Views: 73

Answers (2)

Stephan Lechner
Stephan Lechner

Reputation: 35154

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.

Upvotes: 1

Emil Laine
Emil Laine

Reputation: 42828

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 newed memory.

Upvotes: 1

Related Questions