Initialisation of static structure variables

Discussion in 'C++' started by kselvaakumar@gmail.com, Feb 7, 2007.

  1. Guest

    I have declared a global structure like this:
    struct
    {
    float a;
    float b;
    }static one;

    This is in a separate header file file1.h;
    Now how can i initialise those variables one.a and one.b in another
    cpp file.

    Since it has been declared as static, I couldnt make it as extern.
    Moreover I dont want to declare any object to that structure.All the
    variables one.a and one.b are treated as constants.

    I want to simply use it as one.a and one.b with the structure name
    itself.
    Can u please help in achieving this??

    Thanks
    Selva
    , Feb 7, 2007
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > I have declared a global structure like this:
    > struct
    > {
    > float a;
    > float b;
    > }static one;
    >
    > This is in a separate header file file1.h;
    > Now how can i initialise those variables one.a and one.b in another
    > cpp file.
    >
    > Since it has been declared as static, I couldnt make it as extern.


    Since it is static, each file that includes file1.h will have its own copy.
    Maybe you meant:

    struct one
    {
    static const float a;
    static const float b;
    };

    ?

    > Moreover I dont want to declare any object to that structure.


    Then don't.

    > All the variables one.a and one.b are treated as constants.


    Why do they have to be in a struct then? Just use a namespace.

    namespace one
    {
    const float a;
    const float b;
    }
    Rolf Magnus, Feb 7, 2007
    #2
    1. Advertising

  3. John Carson Guest

    <> wrote in message
    news:...
    >I have declared a global structure like this:
    > struct
    > {
    > float a;
    > float b;
    > }static one;
    >
    > This is in a separate header file file1.h;
    > Now how can i initialise those variables one.a and one.b in another
    > cpp file.


    If it was done in another .cpp file, it would be assignment, not
    initialisation.

    When you make variables static, you get a separate copy of them in each
    translation unit in which your header file is included. Thus you cannot make
    assignments to all of them using just one .cpp file (you can't do it in
    multiple .cpp files either without some difficulty because you need to work
    around C++'s restrictions on your ability to make assignments outside of
    main()).

    The best solution is to initialise them in the header file. You can do it as
    per the following example:

    struct
    {
    float a;
    float b;
    }static one = {1.0, 2.0};

    > Since it has been declared as static, I couldnt make it as extern.


    Why have you made it static?

    > Moreover I dont want to declare any object to that structure.


    Why not? You in fact have declared an object. one is an object.

    > All the variables one.a and one.b are treated as constants.
    >
    > I want to simply use it as one.a and one.b with the structure name
    > itself.


    one is not the structure name. It is the name of an object. Rolf has shown
    how to just use the structure name with no objects.


    --
    John Carson
    John Carson, Feb 7, 2007
    #3
  4. On Feb 7, 5:34 am, wrote:
    > I have declared a global structure like this:
    > struct
    > {
    > float a;
    > float b;
    >
    > }static one;
    >
    > This is in a separate header file file1.h;
    > Now how can i initialise those variables one.a and one.b in another
    > cpp file.
    >
    > Since it has been declared as static, I couldnt make it as extern.
    > Moreover I dont want to declare any object to that structure.All the
    > variables one.a and one.b are treated as constants.
    >
    > I want to simply use it as one.a and one.b with the structure name
    > itself.
    > Can u please help in achieving this??
    >
    > Thanks
    > Selva


    It seems to me like you really want to create a single copy of the
    struct that is to be used from multiple .cpp files. In that case, you
    should do this.

    In the .h file:

    struct your_struct_name
    {
    float a;
    float b;
    };

    extern your_struct_name one;

    In the .cpp file:

    // This is the copy of the struct that was declared in the .h file.
    your_struct_name one;

    Now you will have a single copy of the struct, which will be
    accessible anywhere you include the .h file. Notice that if you use
    extern, you don't need to use static. In fact, the compiler will not
    let you use both extern and static at the same time.

    If you want to initialize the values of the struct, then you need to
    add a constructor that initializes the values. You may also want to
    add a default constructor for convenience.

    struct your_struct_name
    {
    float a;
    float b;
    your_struct_name() : a(0), b(0) { } // default constructor
    your_struct_name(float _a, float _b) : a(_a), b(_b) { }
    };

    Then in your .cpp file, you can just do this:

    your_struct_name one(12.34, 56.78);

    Regards,
    Markus.
    Markus Svilans, Feb 7, 2007
    #4
  5. John Carson Guest

    "Markus Svilans" <> wrote in message
    news:
    >
    > If you want to initialize the values of the struct, then you need to
    > add a constructor that initializes the values.


    Can be useful in some contexts, but is not necessary in this one.

    your_struct_name one = {12.34, 56.78};

    will work just fine.

    --
    John Carson
    John Carson, Feb 7, 2007
    #5
  6. "John Carson" <> schrieb im Newsbeitrag
    news:45c9ed53$0$1147$...
    > "Markus Svilans" <> wrote in message
    > news:
    >>
    >> If you want to initialize the values of the struct, then you need to
    >> add a constructor that initializes the values.

    >
    > Can be useful in some contexts, but is not necessary in this one.
    >
    > your_struct_name one = {12.34, 56.78};


    you are initializing floats with double values.

    So it should be
    your_struct_name one = { 12.34F, 56.78F };

    Georg


    >
    > will work just fine.
    >
    > --
    > John Carson
    >
    Georg Krichel, Feb 8, 2007
    #6
    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. Tim
    Replies:
    2
    Views:
    1,496
  2. John Carson
    Replies:
    2
    Views:
    327
    John Carson
    Jan 23, 2004
  3. clilley
    Replies:
    6
    Views:
    366
    Howard Hinnant
    Nov 18, 2004
  4. Raghu

    Structure initialisation

    Raghu, May 16, 2006, in forum: C++
    Replies:
    3
    Views:
    287
    Ian Collins
    May 16, 2006
  5. Tim Clacy
    Replies:
    8
    Views:
    384
    Tim Clacy
    May 30, 2006
Loading...

Share This Page