0

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

Community
  • 1
  • 1
Lavi
  • 13
  • 2
  • 2
    Please read http://stackoverflow.com/help/how-to-ask – Mgetz Jul 15 '13 at 14:38
  • Which point of 'how to ask' I have violated ? Question is specific. I've reaseched over internet about this problem what else ? – Lavi Jul 15 '13 at 14:42
  • possible duplicate of [Ambiguous class inheritance](http://stackoverflow.com/questions/12793651/ambiguous-class-inheritance) – Mgetz Jul 15 '13 at 14:47
  • How do you want these variables to be "merged"? When you reference the variable `Text` in a method of `Textbox`, which one should the compiler use? They cannot simply be both merged, since `TextEditor::Text` is used for something different than `Caption::Text`. If you would merge these variables, methods of `Caption` could influence the value of `TextEditor::Text` – Alexander Weinert Jul 15 '13 at 14:49
  • @Mgetz - Diamond problem has been sorted already in my case. I've said that in my question. This problem can be considered as an extension of that problem – Lavi Jul 15 '13 at 14:57
  • @Alexander Weinert - This exaclty what I want :) – Lavi Jul 15 '13 at 14:58
  • @Lavi please check out http://meta.stackexchange.com/questions/100278/how-can-i-improve-my-question-when-there-is-no-feedback-left-by-downvoters also http://tinyurl.com/so-hints – Mgetz Jul 15 '13 at 15:00
  • I up voted but you should improve your question. I read many times but still do not quite understand what do you want to do with those variables. – khajvah Jul 15 '13 at 15:04
  • @khajvah The problem may be in the word I've used 'merged'. I want to achieve the thing which 'Alexander Weinert' said at the end of his comment. If I use variables, methods of `Caption` it will influence the values of `TextEditor`. – Lavi Jul 15 '13 at 15:20
  • @Lavi it sounds like you've found how you should fix your question, clarity matters a lot, as does showing what behavior you're intending to achieve and what you've tried previously. Don't be afraid of being verbose. [The guide I linked earlier](http://tinyurl.com/so-hints) is really a good way of approaching asking a question. – Mgetz Jul 15 '13 at 15:23
  • @Mgetz But sometimes you may lack of right words especialy if you're not native English speaker or you don't know the words you should use. Thanks :) – Lavi Jul 15 '13 at 15:31

1 Answers1

0

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 { };
    
Iwan Aucamp
  • 1,469
  • 20
  • 21
  • Second option would require from me to redesign whole GUI API. In my case first option couldn't be used either. So the only thing for me is to duplciate `TextEditor` code, strip it apart so it can inherit `Caption`. I wanted to avoid that but sometime you need to take step back in order to move forward. Anyway thanks for your ideas and help :) – Lavi Jul 16 '13 at 00:07
  • If you were using mutator methods throughout this might also have provided an option. – Iwan Aucamp Jul 16 '13 at 00:09