2

I am quite new to C++, so if there is a easier way to get what I want, feel free to tell me...

I've got a header containing constants I need to include everywhere in my code to use them in equations. Stuff like temperature and pressure.. Before I was using a single object with a constant mass.

real massObject          = 7.35619e-25;

Now I want to have more than one mass to be able to use more objects. So I tried to define Elements of an array I created.

const int numObjects     = 1;
double vmassObject[numObjects];
vmassObject[0]           = 7.35619e-25;

Then I found out that it was not possible to define every Element outside a function but I don't want to use a function because I would have to call it everytime. Also passing is no option. Is there a way to define the elements globally?

Thanks

1
  • 1
    You can in fact declare this array inside a function as static, so that it will be initialized only once throughout the lifetime of your program (and not every time the function is called). You can further pass an input argument to the function, telling it which element in the array you want it to return. Thus, you keep this array encapsulated under a narrow interface, with an access level of your choice (here, I have described a simple interface with read-only access level). In addition, if you want to optimize furthermore, you can declare the function inline. Commented Nov 10, 2016 at 15:02

1 Answer 1

2

You can initialize the array:

double vmassObject[numObjects] = {
    7.35619e-25
};

On a related note, you can't put this in a header file that you include in multiple source files. That's because then the array would be defined multiple times, and you can only have one definition in a program.

In the header you can declare the array:

extern double vmassObject[numObjects];

Then put the definition (with the initialization) in a single source file.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! Initialising works, but is not great of course. When i put the definition in a source file it has to be in a function again, which I don't want or am I missing something?
@TK_B Yes it seems you are missing something, but I don't know what. If you copy-paste the definition as shown in my answer you can put it in the global scope of any source file. Remember to define (and initialize) the size constant first.
Wouldn't #IFNDEF resolve using the same header file in multiple files?
@dwllama Header include guards only protect against multiple inclusion in a single translation unit.
@Someprogrammerdude Thanks for the reply. I'm involved with a project that uses #ifndef a lot, but I see I have more to learn about how it works before I am sure to use it correctly. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.