A C++ book text I really really did not understand

=========
this is a part of text from Stroustrup, pg 401 section 15.2.5.1.
=========

What if different derived classes override the same function? This is allowed if and only if some overriding class is derived from every other class that overrides the function. That is, one function
must override all others.
For example, My_window could override prompt() to improve on what
Window_with_menu provides:

class My_window : public Window_with_menu, public Window_with_border
{
void prompt (); //don't leave user interactions to base
};

If two classes override a base class function, but neither overrides the other, the class hierarchy is an error. No virtual function table can be constructed because a call to that function on the complete object would have been ambiguous.

/*************************************************
this is class window in case someone needs it
/************************************************
(it unedited by should be clear)

class Window {
/ / ...
virtual set_color(color) = 0; //set background color
virtual void prompt() = 0;
};

class Window_with_border : public virtual Window
{
/ / ...
set_color(Color); // control background color
};

class Window_with_menu : public virtual Window
{
/ / ...
void prompt(); //control user interactions
};

class My_window : public Window_with_menu, public Window_with_border
{
/ / ...
};
/*******************************************************8
Consider this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

struct Window
{
    virtual void SetColor()=0;
    virtual void Prompt()=0;
    virtual ~Window(){}
};

struct BorderWindow : virtual public Window
{
    void SetColor() {cout << "BorderWindow::SetColor" << endl;}
};

struct MenuWindow : virtual public Window
{
    void Prompt() {cout << "MenuWindow::Prompt" << endl;}
};

struct MyWindow : public BorderWindow, public MenuWindow
{

};

int main()
{
    Window * pw=new MyWindow;

    pw->SetColor();
    pw->Prompt();

    cout << "hit enter to quit...";
    cin.get(); delete pw;
    return 0;
}

It works ok. And it should. Now, consider this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

struct Window
{
    virtual void SetColor()=0;
    virtual void Prompt()=0;
    virtual ~Window(){}
};

struct BorderWindow : virtual public Window
{
    void SetColor() {cout << "BorderWindow::SetColor" << endl;}
};

struct MenuWindow : virtual public Window
{
    void Prompt() {cout << "MenuWindow::Prompt" << endl;}

    //I added this...
    void SetColor() {cout << "MenuWindow::SetColor" << endl;}
};

struct MyWindow : public BorderWindow, public MenuWindow
{
    //... and this
    //void SetColor() {cout << "MyWindow::SetColor" << endl;}
};

int main()
{
    Window * pw=new MyWindow;

    pw->SetColor();
    //PROBLEM!!! Which one???
    //BorderWindow::SetColor() ? or...
    //MenuWindow::SetColor() ?
    //The problem is solved if you uncomment
    //MyWindow::SetColor()

    pw->Prompt();

    cout << "hit enter to quit...";
    cin.get(); delete pw;
    return 0;
}
Last edited on
@m4ster r0shi

"This is allowed if and only if some overriding class is derived from every other class that overrides the function"

what i concluded from this line was :

struct window
{
virtual void SetColor()=0;
virtual void Prompt()=0;
virtual ~Window(){}
};
struct window1 : public virtual window
{
void SetColor() {cout << "window1::SetColor" << endl;}
}
struct window2 : public virtual window
{
void SetColor() {cout << "window2::SetColor" << endl;}
}
struct window3 : public virtual window
{
void SetColor() {cout << "window3::SetColor" << endl;}
}

struct win : public window1, public window2
is not allowed as it doesn't derive from all the three changing the same function SetColor()

was my interpretation wrong ?
I understood what you were saying but did not understand how it pertains to this part of the text from stroustrup i quoted in the first line.

~Cheers!
navder
I opened Stroustrup's book, found the part you are talking about and, well, I can't say your interpretation about that first quote is wrong. That's what he seems to be saying. But this is not correct. My guess is that he didn't mean to say this. In your example above, assuming we provide win with its own SetColor, the only thing that prohibits win's instantiation is the pure virtual function Prompt, which is never implemented.
Last edited on
whats "virtual public" inheritance?
Blessman11 wrote:
whats "virtual public" inheritance?

African Voodoo.

Seriously now, it's the mechanism C++ provides as a solution to the diamond problem
that may occur in multiple inheritance -> http://en.wikipedia.org/wiki/Virtual_inheritance
Last edited on
m4ster r0shi :

http://www.cplusplus.com/forum/beginner/37165/

comment on this plz.

~navderm
Topic archived. No new replies allowed.