Initializing static members

Here is the code that doesn't work
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
#include <iostream>

class test
{
public:
void print()
{
std::cout << _n << std::endl;
}
int& n()
{
return this->_n;
}
private:
static int _n;
};

//int test::_n = 12; // Works fine!

int main()
{
test::_n = 15; // Doesn't work!
test a,b,c;
//a.n() = 10; // Works fine!
a.print();
b.print();
c.print();
return 0;
}
The line 22 is the problem. How can I initialize a static member of a class without instantiating the class first. Line 18 is not what I'm after, not is the line 24. I'd prefer to initialize test::_n in main().
Line 18 is how it's done. Static members must already be constructed before main is entered. It would make no sense to initialize anything in main.
What if I want to allow the user to input the value _n and then create the objects?
1
2
std::cin >> test::_n;
test a,b,c;
That will work. The only issue is that _n would have to be public, then, since you're accessing it outside the class.

Either that, or make a static accessor function for it.
1
2
static int& n()
{return _n;}
Adding a function like the one above doesn't seem to work. How does this static accessor function should look like?
Last edited on
Adding a function like the one above doesn't seem to work. How does this static accessor function should look like?

It looks exactly like that. Besides, "does not work" is not a valid problem description and never was.
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
32
33
#include <iostream>

class test
{
public:
void print()
{
std::cout << _n << std::endl;
}
int& n()
{
return this->_n;
}
static int& nset()
{
return _n;
}
private:
static int _n;
};

int test::_n = 12; // Works fine!

int main()
{
test::nset() = 15; // Works if line 22 is uncommented!
test a,b,c;
//a.n() = 10; // Works fine!
a.print();
b.print();
c.print();
return 0;
}
Works only if line 22 is uncommented. Does that I am still doing something wrong?
Works only if line 22 is uncommented. Does that I am still doing something wrong?

No! How is it supposed to "work" when you don't provide a definition for the static member?
So let me get that strait. static int _n; in line 19 is not a definition, it is just a declaration, is that correct?
Does that mean that static members of a class are in fact global variables, but their scope has been limited to the class in which they have been defined?
Yes, both is correct.
OK, how about this code
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
#include <iostream>

class test
{
public:
int& n()
{
return this->_n;
}
private:
int _n;
};

class tester
{
public:
static test a;
}

test tester::a;

int main()
{
//tester::a.n() = 3;
tester a,b,c;
return 0;
}
I get an error saying 20: error: expected initializer before 'tester'
Last edited on
There is a semicolon missing in line 18.
Now I'd like to encapsulate the data members of the class tester so I'm trying something like this
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

class test
{
public:
int& n()
{
return this->_n;
}
int const& n() const
{
return this->_n;
}
private:
int _n;
};

class tester
{
public:
test& a()
{
return this->_a;
}
test const& a() const
{
return this->_a;
}
public:
static test _a;
};

test tester::_a;

std::ostream& operator<<(std::ostream& os, tester const& obj)
{
os << obj.a().n() << '\n';
return os;
}

int main()
{
tester::_a.n() = 5; // Works!
tester::a().n() = 5; // Doesn't work!
tester a,b,c;
std::cout << a << b << c << std::endl;
return 0;
}
But I get
In function 'int main()':
44: error: cannot call member function 'test& tester::a()' without object
Last edited on
You can't call a non-const member function for a const object.
You need a second, const variant of n(): const int& n() const {return _n;}
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

class test
{
public:
int& n()
{
return _n;
}
int const& n() const
{
return _n;
}
private:
int _n;
};

template<typename T>
class tester
{
public:
test& a()
{
return _a;
}
test const& a() const
{
return _a;
}
public:
static test _a;
};

template<typename T> test tester<T>::_a;

template<typename T>
std::ostream& operator<< (std::ostream& os, tester<T> const& obj)
{
os << obj.a().n() << '\n';
return os;
}

int main()
{
tester<double>::_a.n() = 5;
tester<int>::_a.n() = 1;
tester<double>::a().n() = 5; // Doesn't work!
tester<double> a,b;
tester<int> c;
std::cout << a << b << c << std::endl;
return 0;
}

How can I deal with that?
In function 'int main()':
47: error: cannot call member function 'test& tester<T>::a() [with T = double]' without object
Here is a working code
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

class test
{
public:
int& n()
{
return _n;
}
int const& n() const
{
return _n;
}
private:
int _n;
};

template<typename T>
class tester
{
public:
static test& aa()
{
return _a;
}
test const& a() const
{
return _a;
}
public:
static test _a;
};

template<typename T> test tester<T>::_a;

template<typename T>
std::ostream& operator<< (std::ostream& os, tester<T> const& obj)
{
os << obj.a().n() << '\n';
return os;
}

int main()
{
tester<double>::_a.n() = 5;
tester<int>::_a.n() = 1;
tester<double>::aa().n() = 5;
tester<double> a,b;
tester<int> c;
std::cout << a << b << c << std::endl;
return 0;
}
Topic archived. No new replies allowed.