Class pointer needs to be initialized in initializer list? Also why not "this->"?

I have two custom classes, Font and GlyphStr. Font stores info about glyph sizes and textures and so on, while GlyphStr stores an array of vertex positions and uv coordinates that represent a specific string. The GlyphStr class accesses a Font object when it's constructing its array, to get info about glyph sizes and other things. I don't want to have multiple copies of the same Font object, so I have each GlyphStr object contain a pointer to the Font object it's to use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Font
{
public:
	Font();
	~Font();
	bool loadFont(std::string path);
	int getHorizOffset(char which);
private:
	std::vector<int> glyphOffsetArray;
};

class GlyphStr
{
public:
	GlyphStr();
	~GlyphStr();
	void initGlyphStr(std::string text);
	void setFont(Font &which);
private:
	std::vector<vertices> verts;
	Font &font;
};

I can guarantee that initGlyphStr, which accesses the GlyphStr's Font's info, won't be called until after that particular GlyphStr's Font pointer is set to point to a valid, initialized Font object that I handled elsewhere in the code. As long as that's true, it doesn't seem to me like there should be a problem, but Visual Studio spits out an error: "'GlyphStr::font' : must be initialized in constructor base/member initializer list".

I found a SO question similar to mine at http://stackoverflow.com/questions/15461081/must-be-initialized-in-constructor-base-member, where it was suggested to use initializer lists, so that Font's constructor is called for every GlyphStr object, before that GlyphStr's own constructor. But that seems to defeat my purpose; I tried to avoid initializing a separate Font object for every GlyphStr, since many GlyphStrs might use the same Font. Is there some other way I could consistently access a single particular Font object's data when calling an individual GlyphStr object's methods?


Also, the SO discussion I found contained the advice "avoid the use of 'this->' in C++". Why is it a good idea to avoid "this->"?
You can't reseat a reference. A reference must be initialized when it's constructed.

For example:

1
2
3
4
int var;

int& ref;  // <- ERROR, you have to make the reference refer to something IMMEDIATELY 
ref = var; // <- this won't work 


In a class, it's the same. Your 'font' reference in your GlyphStr class needs to be initialized when the GlyphStr class is constructed... or else you'll never be able to make it refer to anything.

So you have 2 options:

1) Get rid of the setFont function.. and instead, pass the font in as a parameter to the constructor, so you can set the font reference immediately.

or

2) Change 'font' to be a pointer rather than a reference. Pointers can be reseated and do not have to be initialized immediately.
I would want to keep the setFont function if I could. Changing font to a pointer worked, thanks. I guess I was confused as to the difference between a reference and a pointer, time to hit the books.
Topic archived. No new replies allowed.