How Would One Initialise Members of an Unknown Type?

closed account (zb0S216C)
Consider this:

1
2
3
4
5
6
7
8
struct Base
{
    Base( );
    struct 
    {
        int A;
    } Members;
};

Since the structure in Base isn't a type, how would one initialise Base::A? Base's constructor can't do it because A isn't a member of Base.

Ideas?

Wazzak
Last edited on
Wouldn't it be Base.Members.A? A technically isn't a member of B, it is a member of Members.
Last edited on
closed account (o3hC5Di1)
Hi there Framework,

On the chance of providing you with totally useless info - this is some stuff I found _i think_ might apply to what you're asking:

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=557
http://stackoverflow.com/questions/1972003/how-to-use-anonymous-structs-unions-in-c

Hopefully that helps.

All the best,
NwN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Base
{
    Base()
    {
        Base::Members.A = 10;
    }

    struct
    {
        int A;
    }Members;
};

int main()
{
    Base base;
    std::cout << base.Members.A << std::endl;
}


no?
Last edited on
closed account (zb0S216C)
@Texan40: As I said, A isn't a member of Base, but you're right; A is a member of members.

@Nwn: Thanks for the links. I'll give them a once-over :)

@Zephilinox: It works, but it isn't initialisation :(

Thanks for the responses thus far, lads/ladies (if any) :)

Wazzak
it isn't? what could be more initialized than an int with the value of 10 ._.

what do you mean by initialized? (always get that, declaration, and definition confused)
closed account (zb0S216C)
Zephilinox wrote:
"it isn't? what could be more initialized than an int with the value of 10 ._."

No, it's assignment. Assignment comes after initialisation. By the time you assign a value to a variable, the variable would've been fully initialised.

Zephilinox wrote:
"what do you mean by initialized?"

Initialisation is the term used to describe the assignment of a value to an object/variable at the point of its declaration.

Zephilinox wrote:
"always get that, declaration, and definition confused"
Define and declaration mean the same thing when it comes to variables. However, define and declaration are two different things in the context of classes and functions.

Wazzak
Last edited on
so is this also assignment? if it is how would you normally initialize a member variable?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct Base
{
    Base():
    A(10){}

    struct
    {
        int A;
    };
};


int main()
{
    Base base;
    std::cout <<  base.A << std::endl;
}


this is also my solution #2, but I removed the Members part of the struct, its the only way I could figure out of assigning it in a constructor
Last edited on
That is initialization, because think of this:

1
2
3
4
5
struct Base
{
  Base() : text("foobar") {}
  const char *text;
};


Because you can't "assign" text a value outside of initialization, right?
closed account (zb0S216C)
That's right. Any modifications to a member outside the constructor's initialiser-list is considered assignment. The initialiser-list is the only chance you're going to get to initialise your members. If you didn't know, the initialiser-list sits between the colon (after the constructor's parameter-list), and the opening brace of the constructor.

Any road, I don't think there's a way to initialise A, so I'm gonna' have to assign a value to A inside Base's constructor body.

Thanks for the help, all :)

Wazzak
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>

struct Base
{
  Base() : text("foobar") {assign();}
  const char *text;
  void assign()
  {
     text = "barfoo";
  }
};

int main()
{
    Base base;
    std::cout << base.text << std::endl;
}


I guess you can?
Last edited on
closed account (zb0S216C)
There's no need to validate initialisation; you can ensure the initial value will be the value text holds after initialisation.

Wazzak
yeah, but regardless of that fact I didn't notice the compiler warned me about comparing it with a string literal, forgot it was a char.

also, yeah I do know what an initializer list is, I used it in my second example :3

anyway, sorry I couldn't help.
Last edited on
Topic archived. No new replies allowed.