user3327715
user3327715

Reputation: 1

g++ (GCC) 4.6.0 I have the following class and I am trying to initialize structure member initialization list of my constructor

g++ (GCC) 4.6.0

I have the following class and I am trying to initialize in my initialization list of my constructor.

class Floor_plan
{
private:
    unsigned int width;
    unsigned int height;

    struct floor_size {
        unsigned int x;
        unsigned int y;
    } floor;

public:
    Floor_plan() : width(0), height(0), floor.x(0), floor.y(0) {} {}
    Floor_plan(unsigned int _width, unsigned int _height, unsigned int _x, unsigned int _y);
    ~Floor_plan() {};
}

In the above code,

how can we initialize floor_size structure member without creating object of floor.... Thanks in advance for your answers....

Upvotes: 0

Views: 76

Answers (4)

Ferris Garden
Ferris Garden

Reputation: 53

Floor is already an object of "class"(or struct) floor_size.

You just need an constructor for intiliation.

For Example:

class Floor_plan
{
private:
    unsigned int m_width;
    unsigned int m_height;

    struct floor_size {
        unsigned int m_x;
        unsigned int m_y;
        public: floor_size(int p_x,int p_y) : m_x(p_x),m_y(p_y) {}
    } floor;


public:
    Floor_plan() : m_width(0), m_height(0), floor(0,0) {}
    Floor_plan(unsigned int _width, unsigned int _height, unsigned int _x, unsigned int _y);
    ~Floor_plan() {};
};

Upvotes: 0

Balayesu Chilakalapudi
Balayesu Chilakalapudi

Reputation: 1406

if you use static keyword infront of struct floor_size, you can initialize by using the class name like this,

Floor_plan::floor.x=100;
Floor_plan::floor.y=100;

Upvotes: 0

danadam
danadam

Reputation: 3450

Your struct floor_size is POD (Plain Old Datatype) so you can initialize floor member like this:

Floor_plan() : width(0), height(0), floor()
{}

This will use one of zero-, default- or value-initialization, I never know which.

And if you enable C++11 support (-std=c++0x switch) you can use brace initialization:

Floor_plan() : width(0), height(0), floor({10, 20})
{}

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

I think that you need not to initialize this structure. Simply define it as a type member

struct floor_size {
    unsigned int x;
    unsigned int y;
};

And when you will need to reinterpret width and height as elements of floor_size you can create an object of floor_size as for example

floor_size floor = { width, height };

Upvotes: 0

Related Questions