0

I want to initialise a lot of variables (doubles and ints mostly, but some vectors as well), which will then be passed into lots of functions. What is the best way to initialise these?

I could initialise them all in the main function, this just feels a little messy, and ideally I wanted to create a separate function to initialise them in. I want to try and keep the main function as empty as I can. However, I then have the issue of not being able to return them.

Is there a proper way of doing this? I've heard of constructors, but I'm not entirely sure what they're used for, and whether or not they're something that I should be looking at here.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user112495
  • 184
  • 9
  • 4
    Do learn constructors. – Maxim Egorushkin Nov 04 '19 at 15:51
  • 3
    I recommend reading about classes and how to structure your data. [Just any beginner's C++ book will do](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Btw - C++ and Python are only OO if you want them to (and you most probably should). – nada Nov 04 '19 at 15:51
  • I do intend to learn about constructors - I just learn better by working on projects, and learning about those things where I get to a point in the project that would benefit by knowing about those things. So are constructors the thing I want to use here? Or are they just something generally useful to know, but probably not the specific thing I'm looking for here? – user112495 Nov 04 '19 at 15:53
  • 1
    You need ctors all over the place. – nada Nov 04 '19 at 15:54
  • To learn constructors and all you need to get into classes. For time-being if you want separate function, just create it and pass arguments as references and also python or C++ doesn't matter. If you are limiting yourself with language then you cann't grow. They are just tools. – Nikhil Badyal Nov 04 '19 at 15:54
  • 1
    Even if it turns out that constructors aren't the solution to your problem, learn constructors. [They are almost immediately useful.](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) – user4581301 Nov 04 '19 at 15:54
  • @user4581301 That link seems far too early. – nada Nov 04 '19 at 15:56
  • You may wanna look into [Builder Pattern](https://en.wikipedia.org/wiki/Builder_pattern) too. It often simplifies the construction of big and complicated objects. – Amir Nov 04 '19 at 15:57
  • Just be aware that C++ is maybe as hard to learn as Python, but far more difficult to master. Push through. The rewards are also higher. – nada Nov 04 '19 at 15:58
  • It might be, @nada , but RAII is the magic sauce that makes lot of C++ work. The sooner you learn, the sooner you get to take advantage. – user4581301 Nov 04 '19 at 16:00
  • @problematicDude As of now, I have used C++ a bit, just not in an OO way. I've essentially been using it in the same way I use python (but with a couple of new things such as passing by reference etc.). I have some code (just over 1000 lines) that I'm trying to neaten up and make more modular, before extending it further. Are classes what I would want to use to do this? (Or is it impossible to answer without further context?) – user112495 Nov 04 '19 at 16:02
  • The solution is just a temporary solution. Classes are a must in C++. C++ exist because of classes. You must learn them. Just go there and pick any book as per your convenience. https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Nikhil Badyal Nov 04 '19 at 16:05

2 Answers2

0

1) you can use function, pass variable by reference for initialisation to keep main function clean.

2) you can use templates as mention in this video .

See the code.

Mayank Dutta
  • 3
  • 1
  • 5
0

Based on your requirements, I would use structs with default values:

struct MyConfiguration {
    double someValue{ 5. };
    int someOtherValue{ 3 };
};

int main( int, char ** ) {
    const MyConfiguration c;

    f( c.someValue );
    g( c.someValue, c.someOtherValue );
}

For many kinds of applications it would be reasonable to read those values from a configuration file.

Markus Mayr
  • 4,038
  • 1
  • 20
  • 42