Example - Static Const vector<vector>

Many People ask me all the time about how initialize a static vector, const vector, etc...
So here is a really complex example, and this way always works. So don't ask again :)

Atype.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Atype
{

public:
	Atype(Btype vB):
	 mB(vB)
	 {}
	virtual ~Atype(void){}

	const Btype& getB(void) const { return mB; }
	//...

private:
	Btype mB;
	//...
};


Ctype.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Ctype
{

public:
	Ctype(){}
	virtual ~Ctype(void){}

	struct Values{
		Atype vA; 
		//...
		Values(a) : 
			vA(a) {}
	}
};

private:
	static std::vector< std::vector<Values> > getAVector();
	static const std::vector< std::vector<Values> > aVector;
};


Ctype.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const std::vector< std::vector<Ctype::Values> > Ctype::aVector = Ctype::getAVector();

std::vector< std::vector<Ctype::Values> > Ctype::getAVector()
{
	std::vector< std::vector<Values> > aV;
	std::vector <Values> aV1, aV2;
	
	// Let's say Btype contains 2 string
	aV1.push_back(Values(Atype(Btype("a","b"));
	aV1.push_back(Values(Atype(Btype("c","d")); 

	aV2.push_back(Values(Atype(Btype("a","e")); 
	aV2.push_back(Values(Atype(Btype("a","f")); 

	aV.push_back(aV1);
	aV.push_back(aV2);

	return aV;
}


After this you can get the Btype mB data fast and const with getB()
Last edited on
Really? Where's Btype?
Topic archived. No new replies allowed.