C++ Trouble finding the right class hierarchy implementation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mr5zz
    New Member
    • May 2013
    • 2

    C++ Trouble finding the right class hierarchy implementation

    Please see first the code:


    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:

    Code:
        BMfont::BMstring text;
        text.scale //I don't want this
    What I want to do here is, I want the `BMstring::Comp ile()` to have an access to `BMfont` with no any instance of `BMfont` inside `BMstring`.

    -----

    Or what if I do this:

    Code:
        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?
    Last edited by acoder; May 29 '13, 11:51 AM. Reason: Please use [code] tags when posting code
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    mr5zz,

    How can I do `BMstring` have an access to `BMfont`'s members, as if `BMstring` will not inherit `BMfont`'s members?
    Have you tried having BMfont declare BMstring as a friend?
    Code:
    friend class BMstring;
    Or, have i missed the point?

    Cheers,
    Oralloy

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      BMString can not have access to BMFont members because just because an instance of BMString exists does not mean that an instance of BMFont exists.

      Anyone can declare a variable of type BMString.

      Somehow you need to enable BMString have visibility of the BMFont you wish it to using while it is compiling so for example perhaps BMString::compi le() should be declared BMString::compi le(const BMFont& font).

      Whatever method you choose you will need to get a BMFont object or reference in scope inside the BMString object.

      Also it is very poor practice to have BMFonts data members public.

      Comment

      Working...