How to access constructor for unamed structure?

J

Jim Langston

What I want to do:

class MyClass
{
public:
struct
{
bool Enable;
bool Velvety;
float Density;
Fog(): Enable(false), Velvety(true) {}; // Won't work
} Fog;
};

The above won't work because the structure is not, in fact, named Fog, but
is an unnamed structure. I know I can give the structure a name I.E. struct
SFog, and use SFog(): Enable(false)... but I would rather be able to
override the constructor of the unnamed structure.

Is there some mechanism to do this?
 
B

BigBrian

Jim said:
What I want to do:

class MyClass
{
public:
struct
{
bool Enable;
bool Velvety;
float Density;
Fog(): Enable(false), Velvety(true) {}; // Won't work
} Fog;
};

The above won't work because the structure is not, in fact, named Fog, but
is an unnamed structure. I know I can give the structure a name I.E. struct
SFog, and use SFog(): Enable(false)... but I would rather be able to
override the constructor of the unnamed structure.

Is there some mechanism to do this?

Why not just give the struct a name, ie

struct Fog
{
....
};

or

class Fog
{
....
};
 
C

Christian Meier

Jim Langston said:
What I want to do:

class MyClass
{
public:
struct
{
bool Enable;
bool Velvety;
float Density;
Fog(): Enable(false), Velvety(true) {}; // Won't work
} Fog;
};

The above won't work because the structure is not, in fact, named Fog, but
is an unnamed structure. I know I can give the structure a name I.E. struct
SFog, and use SFog(): Enable(false)... but I would rather be able to
override the constructor of the unnamed structure.

Is there some mechanism to do this?
No. You can't.
But when you put your bools in another (named) struct, then you can define
this constructor:

class MyClass
{
public:

struct Wrapper
{
bool Enable;
bool Velvety;
Wrapper(): Enable(false), Velvety(true) {};
}

struct
{
Wrapper m_myWrapper;
float Density;
} Fog;
};


AFAIK, the compiler will give your unnamed struct a name and will generate a
default constructor for it - which calls the constructor of the struct
Wrapper.

Greetings Chris
 
J

Jim Langston

Christian Meier said:
No. You can't.
But when you put your bools in another (named) struct, then you can define
this constructor:

class MyClass
{
public:

struct Wrapper
{
bool Enable;
bool Velvety;
Wrapper(): Enable(false), Velvety(true) {};
}

struct
{
Wrapper m_myWrapper;
float Density;
} Fog;
};


AFAIK, the compiler will give your unnamed struct a name and will generate
a
default constructor for it - which calls the constructor of the struct
Wrapper.

That looks worst than what I'm currently doing:

class CClient
{
public:

struct SFog
{
bool Enable;
bool Velvety;
JCOLOR Color;
float Density;
SFog(): Enable(false), Velvety(true) {};
} Fog;
};

I guess I'll have to stick with using named structures, although it seems
kinda a waste using a name just to specify the constructor.

Thanks.
 
N

Niklas Norrthon

That looks worst than what I'm currently doing:

class CClient
{
public:

struct SFog
{
bool Enable;
bool Velvety;
JCOLOR Color;
float Density;
SFog(): Enable(false), Velvety(true) {};
} Fog;
};

I guess I'll have to stick with using named structures, although it seems
kinda a waste using a name just to specify the constructor.

Can you tell us where in the waste lies? Is it really so bad having to type
four characters extra? If so, you can save some keystrokes by calling the struct
'a' or 'b' or '_' or any other name one character long (the cost is readability
of course).

The full name of the struct above is ::Client::SFog, so you are still free to
use unqualified SFog for other entities in other contexts.

/Niklas Norrthon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top