0

Please see first the code:

class BM_FONT_CALL BMfont
{
public:

BMfont();
~BMfont();

bool Load(const std::string& fontName);
void Print(float x, float y);

class BM_FONT_CALL BMstring : public std::string
{

public:

    BMstring() { }
    BMstring(const char* str);

    BMstring& operator=(const char* str);
    BMstring operator+=(const char* str);

private:

    void Compile();

};

public:

BMstring text;
float scale;
_uint32 tabSize;
_uint32 textureSheet;
_uint32 backTexture;
_uint32 frontTexture;
bool enableMasking;

_uint32 base;
_uint32 lineHeight;
_uint32 pages;
_uint32 scaleW, scaleH;
_uint32 kerninfo_count;

BMkerninfo  *kerninfo;
BMchar      chars[MAX_CHAR];

private:

std::string _fontName;

};

How can I do BMstring have an access to BMfont's members, as if BMstring will not inherit BMfont's members? For example, if I do this:

BMfont::BMstring text;
text.scale //I don't want this

What I want to do here is, I want the BMstring::Compile() to have an access to BMfont with no any instance of BMfont inside BMstring.


Or what if I do this:

class BM_FONT_CALL BMstring : public std::string
{

    std::function<void (void)> func;

public:

    BMstring() { func = BMfont::Compile(); }

}

By making the Compile() member of BMfont. But this won't compile. How can I achieve this?

mr5
  • 3,438
  • 3
  • 40
  • 57

2 Answers2

0

The easiest and cleanest way is to have a reference within BMString, and pass it into the constructor or into the compile method. Without a reference, the BMfont and BMstring objects would have to be coupled in memory, with each font having exactly one string - this is surely not desired.

thiton
  • 35,651
  • 4
  • 70
  • 100
  • Can you provide an example source code? I didn't get it. And please look again at the post, I made an edit. – mr5 May 29 '13 at 07:31
  • Okay I get it now. The reason why I derived `BMstring` from `std::string` is because I want to override `std::string`'s assignment operator and put there the `Compile()` function. And by making a reference to `BMfont` I cannot do this `text = "some string";` – mr5 May 29 '13 at 07:58
  • in that case you might want a 'default' font that the string could use, this could be a static member of either BMFont or BMString that your BMString function could access. – matt May 29 '13 at 08:05
0

As I understand you want to do something like this:

class C{
public:
    C() : nested(*this)
        {
        }

    void callNested()
        {
            nested.callOuter();
        }

 private:
    class N{
    public:
        N(C &c) : outer(c)
            {
            }

        void callOuter()
            {
                outer.OuterFunc();
                // you have access to any C's member through outer 
                // reference
            }

    private:
        C &outer; 
    };

    N nested;

    void OuterFunc()
        {
        }
};

int main()
{
    C c;
    c.callNested();
}
Terenty Rezman
  • 357
  • 2
  • 12