variables, that you can use in dif. classes

hello,
I would like to create a lot of variables, that you can use in different classes. If you create the classes inside your main, then you just have to write the variables outside the main (). But if you have classes outside your main.cpp, how can you do it then? You could prototype it, but if I create ~500 variables, that could be much to much work. So how do you do it? I should be able to use them in every class.
Those would be global variables, which are big nono's. Rethink your problem, you don't need 100's of global variables.

Speaking of which, what the heck are you trying to do?
I would like to create many strings. Then, i would like to do different things with them. Every other 'thing' i want to do with them will need its own class.
They will all be constant. It would be like a mini-dictionary with some extra options.
Are you sure you don't just need different functions to manipulate them? And if you really need functions you could have one "StringManager" class which passes them to the other classes.
String manager class? How does that work?
Oh, if it's just constant data you'll be fine using a global var. Although a little manager class would still be nicer. You could use something like std::map, and map your strings and their associated data to specific IDs. Although if you just number them a vector would be much faster.

One of the advantages to such a manager is that you could write something like a LoadFromFile function to load your data from a text file or something of the sort. Also, you could implement sorting functions and such.

And about my first post about GLOBALS ARE EVIL, they obviously do have their uses (As proven by this post...) but many newcomers to C++ greatly abuse them, so I try to steer them away.

The reason globals are considered bad in most cases is that they can be changed anywhere in the code, so if you have an important global declared somewhere, any part of your code can access it and change it, which can lead to huge bugs,
At first I wanted to use globals(I don't know how to make one yet!) but you changed my mind. I want to make a maneger class. How do you make one?

(This question 2 does NOT mean that I don't want to know how to create a manager class!) I also tried to make it by inheritance, but if I try to cout a variable from the other class, the programm wont recognise the var.
> I would like to create a lot of variables, that you can use in different classes.

Have these as variables with a protected access specifier in a common base class, perhaps?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>
#include <cstdlib>

struct use_variables
{
    protected:
       static std::string name ;
       static constexpr int value = 78 ;
       static const int salt ;
};

std::string use_variables::name = "gjghkikh" ;
const int use_variables::salt = std::rand() ;

struct A : use_variables
{
    int foo() const { return i + value + name.size() * salt ; }
    int i = 8 ;
};

struct B : use_variables
{
    int bar() const { return j + value - salt ; }
    int j = 89 ;
};

struct C : use_variables
{
    std::string baz() const { return name + s ; }
    std::string s = "!!!" ;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct StringData
{
//Data
};

class Manager
{
public:
Manager(){}
const& StringData getString(const unsigned ID);
const& StringData findString(const& std::string);
int LoadData(const std::string = "");
private:
std::vector<StringData> data;
std::map<std::string, StringData> dataMap
};


Kind of like that. Obviously that's a very basic setup. I included both vector and map, because map would be easier to find the actual string in (logn), whereas vector is better for an index or ID (constant).

The functions return const variables so that your program can't change them.

As for your second question, you would either have to make the variables that need to be shared static (The variable is created on constructing the first instance of that class, and shared across every instance of it) or create a new dictionary for every instance (kind of a waste).

EDIT: Also, passing values by reference is probably faster in this case.
Last edited on
Topic archived. No new replies allowed.