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->"?