Global variables in classes

Hi,

I'm currently making a program,which is taking a global variable maxKoeLaengde and uses it in the constructor (this will be a constant defined before compiling), however how do i initialize it? (i would like to do this in main) i could make a friend function to set it, but being that it is used in the explicit constructor this makes it difficult. And i cant set it in main, atleast what Visual Studio says.

If you have an example to how to do it, that would be appreciated.


(class.h, class.cpp and main.cpp are the files used).



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;

class Lifokoe
{
	int maxKoeLaengde;
private:
	int valgtKoeLaengde;
	int ledigPladsNr;
	int elementArray[maxKoeLaengde];

public:
	Lifokoe(int);
	bool indSaetElement(int);
	bool udtagElement(int &);
	bool erKoeTom();
	void sletKoe();
};
My guess is that you want something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Lifokoe
{
private:
	static const int maxKoeLaengde=25; //or any number you like
	int valgtKoeLaengde;
	int ledigPladsNr;
	int elementArray[maxKoeLaengde];

public:
	Lifokoe(int);
	bool indSaetElement(int);
	bool udtagElement(int &);
	bool erKoeTom();
	void sletKoe();
};

I don't think you want/need maxKoeLaengde to be a global variable.
If you want to have access to it from the outside world, you could make it public.
Topic archived. No new replies allowed.