Initialisation of static structure variables

K

kselvaakumar

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
 
R

Rolf Magnus

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;
}
 
J

John Carson

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.
 
M

Markus Svilans

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.
 
J

John Carson

Markus Svilans said:
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.
 
G

Georg Krichel

John Carson said:
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
 

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
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top