Вопрос

This is my first post on stackoverflow so be gentle :)

I have standard diamond problem but I'd managed to sort it out.

class Control  
{ 
  public:  
    bool Focused;  
};

class Caption : public virtual Control  
{
  public:  
    string Text;  
};

class Frame : public virtual Control { };

class Textbox : public Caption, public Frame, public TextEditor { };

Sadly another problem with inheritance appeared. Class TextEditor has common variable names:

class TextEditor
{
  public:
    bool Focused;  
    string Text;  
};

Compiler gives me errors:

ambiguous access of 'Text'

ambiguous access of 'Focused'

But all I want is those variables from all classes to be merged in derived class 'Textbox'.

Here's link to the UML picture with the problem

Thanks for any kind of help Sorry for any languages mistakes and/or question I'm asking.

Update

A little explanation cause I might have use wrong words. Sorry for that.

By 'merge' I meant that:

  • If I use variables or methods of Control, Caption or Frame it will influence the values of TextEditor and vice versa. In other words variables are shared in derived class.

So my final class will look like this:

class Textbox : public Caption, public Frame, public TextEditor  
{  
  public:   
    string Text;  
    bool Focused;  
};

And not like this:

class Textbox : public Caption, public Frame, public TextEditor  
{  
  public:   
    string Caption::Text;   
    bool Caption::Focused;  
    string TextEditor::Text;  
    bool TextEditor::Focused;  
};

Which happening right now. Cause I can't do this:

Textbox A;
A.Text = "Text";

Because I have two variables with the name Text. So I would have to do this:

Textbox A;
A.Caption::Text = "Text";
A.TextEditor::Text = "Text";

Best regards Lavi

Это было полезно?

Решение

Notes:

  1. Consider using mutator methods
  2. Please http://sscce.org/ - your example seems a bit over the show - would be easier to give you a clear example of how to solve your problem if your example in the problem was infact sscce. Either way, its still sufficient and your problem is clear enough I guess.

Answers:

  1. Why does class TextEditor not also inherit from class Control and class Caption ? This would seem like the obvious solution - you would have to use virtual inheritance for class TextBox still though.
  2. If 1 is not an option - then there is this - but in this limited sample HasText and Focusable seems to be pointless when compared to Caption and Control respectively.

    class HasText
    {   
    public:
        virtual string& text() { return text; }
        virtual const string& text() const { return text; }
    private:
        text
    };  
    
    class Focusable
    {   
    public:
        virtual bool& focused() { return focused; }
        virtual const bool& focussed() const { return focussed; }
    };  
    
    class Control : public virtual Focusable { };
    
    class Caption : public virtual Control, public virtual HasText { };
    
    class Frame : public virtual Control { };
    
    class TextEditor : public virtual HasText, public virtual Focusable { };
    
    class Textbox : public virtual Caption, public virtual Frame, public virtual TextEditor { };
    
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top