How to initialize a member of union type

Discussion in 'C++' started by alexwu, Sep 12, 2004.

  1. alexwu

    alexwu Guest

    typedef union _MYUNION

    struct {
    short a;
    short b;
    };
    long c;
    } MYUNION;


    class myclass
    {
    MYUNION myu;
    public:
    myclass();
    };


    // How to initialize myu in the constructor initializer list
    myclass::myclass()
    {
    ....
    }
    alexwu, Sep 12, 2004
    #1
    1. Advertising

  2. "alexwu" <> wrote in message
    news:IdR0d.580920$...
    > typedef union _MYUNION
    >
    > struct {
    > short a;
    > short b;
    > };
    > long c;
    > } MYUNION;
    >
    >
    > class myclass
    > {
    > MYUNION myu;
    > public:
    > myclass();
    > };
    >
    >
    > // How to initialize myu in the constructor initializer list
    > myclass::myclass()
    > {
    > ...
    > }
    >


    You cannot. Unions to not have constructors and therefore cannot be
    initialised; only assigned to.

    john
    John Harrison, Sep 12, 2004
    #2
    1. Advertising

  3. John Harrison wrote in news: in
    comp.lang.c++:

    >
    > "alexwu" <> wrote in message
    > news:IdR0d.580920$...
    >> typedef union _MYUNION
    >>


    >> struct {


    To the OP: Anonymous struct's aren't Standard C++, you *must*
    declare/define something.

    >> short a;
    >> short b;
    >> };
    >> long c;
    >> } MYUNION;
    >>
    >>


    [...]

    >
    > You cannot. Unions to not have constructors and therefore cannot be
    > initialised; only assigned to.
    >


    sed s/Unions/Aggregate types/

    I.e. array's and constructor-free struct's have the same problem.

    However union's with a user defined constructor don't:

    #include <iostream>

    union my_union
    {
    int i;
    double d;
    my_union( int ii ) : i( ii ) {}
    };

    int main()
    {
    std::cout << my_union( 1 ).i << '\n';
    }

    Also you can use *value-initialization* with unions:

    #include <iostream>
    #include <ostream>

    union my_union
    {
    int i;
    double d;
    };

    struct X
    {
    my_union m;
    X() : m() {}
    };

    int main()
    {
    std::cout << X().m.i << '\n';
    }

    Which should, assuming IIUC and its put through a recient Standard
    conforming compiler print 0.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Sep 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    654
    Matt Garman
    Apr 25, 2004
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    854
    Chris Torek
    Apr 26, 2004
  3. Replies:
    3
    Views:
    832
    Peter_Julian
    Oct 10, 2005
  4. Nan Li
    Replies:
    7
    Views:
    541
    Nan Li
    Nov 9, 2005
  5. Lung.S.Wu

    UNION global variabl initialize

    Lung.S.Wu, Sep 12, 2011, in forum: C Programming
    Replies:
    10
    Views:
    829
    James Kuyper
    Sep 14, 2011
Loading...

Share This Page