Static member object

Hello,
I am new in c++ and I am curious as how should I declare an object of a class as a member of another class. When I declared it outside of my class and main as global it worked fine but when I try to declare it as member of a class I get errors. This is my 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cstring>
#include <string>
#include <map>

//A class that keeps count of how many pointers point to each string and handles them.
class StringManager
{
public:

	StringManager() {}

	~StringManager()
	{
		pointer_count.clear();
	}

    //Adds or increments a certain element in the map.
	std::string* AddString(std::string* a_Data)
	{

		if (pointer_count.empty())
		{
			pointer_count.insert(std::pair<std::string*, int>(a_Data, 1));
			return a_Data;
		}
		else
		{
			std::string* p_str;
			std::string str;
			std::string data;
			data = *a_Data;

			for (Map_Type::iterator iter = pointer_count.begin(); iter != pointer_count.end(); ++iter)
			{
				p_str = iter->first;
				str = *p_str;
				if (data.compare(str) == 0)
				{
					a_Data = iter->first;
					++iter->second;
				}
				else
				{
					pointer_count.insert(std::pair<std::string*, int>(a_Data, 1));
				}
			}
			return a_Data;
		}
	}

    //Deletes or decrements a certain map element.
	void DeleteString(std::string* a_Data)
	{
		for (Map_Type::iterator iter = pointer_count.begin(); iter != pointer_count.end(); ++iter)
		{
			if (iter->first == a_Data)
			{
				if (iter->second == 1)
				{
					pointer_count.erase(iter);

				}
				else
				{
					--iter->second;
				}
			}
		}
	}

	void PrintString(std::string* a_Data)
	{
		for (Map_Type::iterator iter = pointer_count.begin(); iter != pointer_count.end(); ++iter)
		{
			std::cout << "String " << *iter->first << " has : " << iter->second << " pointer(s), the address is: " << iter->first << std::endl;
		}
	}

private:
	friend class StringHolder;
	typedef std::map<std::string*, int> Map_Type;
	Map_Type pointer_count; //A map that keeps track of the pointers.

};//class StringManager

//static StringManager g_StrMgr;

//A class that keeps a pointer to a string object
class StringHolder
{
public:

	StringHolder(std::string a_Data)
	{
		m_Data = &a_Data;
		m_Data = g_StrMgr.AddString(m_Data);
	}

	void operator+=(std::string rhs)
	{
	    std::string* newStr = new std::string (*m_Data);
	    (*newStr).append(rhs);
	    g_StrMgr.AddString(newStr);
	    g_StrMgr.DeleteString(m_Data);
		m_Data = newStr;
	}

	~StringHolder()
	{
		g_StrMgr.DeleteString(m_Data);
	}

	void Print()
	{
		g_StrMgr.PrintString(m_Data);
		std::cout << "\n" << std::endl;
	}

private:
	std::string* m_Data;
	static StringManager g_StrMgr;

};//class StringHolder


int main()
{
	std::string test_a = "toli";
	std::string test_b = "vago";
	std::string test_c = "toli";
	StringHolder a(test_a);
	a.Print();
	StringHolder b(test_a);
	b.Print();
	StringHolder c(test_b);
	c.Print();
	b += "vago";
	b.Print();
	StringHolder d(test_c);
	d.Print();
	StringHolder e(test_c);
	e.Print();
	return 0;
}


Plus do I have a memory leak? Wherever I try to delete my new string I get errors.
Last edited on
Topic archived. No new replies allowed.