virtual inherite

G

Guest

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// --- from librarry, can't edits thoes 4 lines below ---

class in1 { public: };
class in2 : public in1 { public: virtual void Show() { } };
class wind : public in2 { public: };
class frame : public wind { public: };

// --- we can edit only below ---

// my extension to wind - adding some handy functions to it
class windExt : public virtual wind { public: virtual void Foo() { } };

// my cutsome frame, that is both an extended window, and a frame
class myFrame : public virtual windExt, public virtual frame {
public:
void Bar() {
Foo();
Show();
}
};

int main() {

myFrame f;

return 0;
}

x.cpp: In member function 'void myFrame::Bar()':
x.cpp:20: error: reference to 'Show' is ambiguous
x.cpp:8: error: candidates are: virtual void in2::Show()
x.cpp:8: error: virtual void in2::Show()


How to fix it?
 
G

Guest

Rafał Maj Raf256 wrote:

Or, better yet:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// --- from librarry, can't edits thoes 4 lines below ---

class in1 { public: };
class in2 : public in1 { public: virtual void Show() { } };
class wind : public in2 { public: };
class frame : public wind { public: };

// --- we can edit only below ---

// my extension to wind - adding some handy functions to it
class windExt : public virtual wind { public: virtual void Foo() { } };

// my extended frame, it is extended in sam way as windExt is (contain
// the Foo() function, but it is alos a frame not just a wind
class frameExt : public virtual windExt, public virtual frame {
....
};
// my cutsome frame, that is both an extended window, and a frame
class myFrame : public virtual windExt, public virtual frame {

class myFrame : public frameExt {
public:
void Bar() {
Foo();
Show();
}
};

and simmilar problem
 
M

Marco Wahl

Rafał Maj Raf256 said:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// --- from librarry, can't edits thoes 4 lines below ---

class in1 { public: };
class in2 : public in1 { public: virtual void Show() { } };
class wind : public in2 { public: };
class frame : public wind { public: };

// --- we can edit only below ---

// my extension to wind - adding some handy functions to it
class windExt : public virtual wind { public: virtual void Foo() { } };

// my cutsome frame, that is both an extended window, and a frame
class myFrame : public virtual windExt, public virtual frame {
public:
void Bar() {
Foo();
Show();
}
};

int main() {

myFrame f;

return 0;
}

x.cpp: In member function 'void myFrame::Bar()':
x.cpp:20: error: reference to 'Show' is ambiguous
x.cpp:8: error: candidates are: virtual void in2::Show()
x.cpp:8: error: virtual void in2::Show()


How to fix it?

What about being specific in this case? I mean the
following change:

[[[
class myFrame : public virtual windExt, public virtual frame {
public:
void Bar() {
Foo();
in2::Show();
}
]]]

Does this work? Is this enough?


HTH
 
G

Guest

What about being specific in this case? I mean the
following change:
void Bar() {
Foo();
in2::Show();
}

Yes, that do work, but I don't want that, My extra class is just adding
more functions to the wind/frame - I do not want to have to use in2:...
or frame:.. in all the derivide classes just because I added few
functions to wind.

Currently I have a workaround: I just wrote simple extend class
(windExt), that do NOT inherit from wind, but only holds a pointer to it.

So, the frame is as usuall, in1->in2->wind->frame
the extention is a separated class windExt with wind *member,
it implements it's additional functions like
windExt:Foo() { member->Something(); .... }

and my finall class is like class FrameExt : public frame, public windExt {}


Is this a good solution? I had to create additional pointer.
 
C

Chris Theis

Rafal Maj Raf256 said:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// --- from librarry, can't edits thoes 4 lines below ---

class in1 { public: };
class in2 : public in1 { public: virtual void Show() { } };
class wind : public in2 { public: };
class frame : public wind { public: };

// --- we can edit only below ---

// my extension to wind - adding some handy functions to it
class windExt : public virtual wind { public: virtual void Foo() { } };

// my cutsome frame, that is both an extended window, and a frame
class myFrame : public virtual windExt, public virtual frame {
public:
void Bar() {
Foo();
Show();
}
};

int main() {

myFrame f;

return 0;
}

x.cpp: In member function 'void myFrame::Bar()':
x.cpp:20: error: reference to 'Show' is ambiguous
x.cpp:8: error: candidates are: virtual void in2::Show()
x.cpp:8: error: virtual void in2::Show()


How to fix it?

Draw a little diagram that shows the inheritance tree of your classes.
You'll see that that the way to Show() is not clear and you have to specify
clearly which version to take, e.g.:

void Bar() {
Foo();
in2::Show();
}

However, in this case it should be seen as a warning that your design is
flawed because your frame is an extended window (being a child of the
original window) and a frame (which is a child of the original window) at
the same time. You're starting to mix & combine different levels of the
hierarchy which I would suggest to avoid. Derive a WindowExt class and
derive a FrameExt from the WindowExt class. Your myFrame calls should
finally be derived from FrameExt and everything is in order.

HTH
Chris
 
M

Marco Wahl

[lot of ommission because I want to focus on one thing only...]
have a workaround: I just wrote simple extend class
[...] that do NOT inherit from [...] but only holds a pointer to it. [...]
Is this a good solution? I had to create additional pointer.

Yes as far as I see it's good. It works. When this
additional indirection *really* causes a problem (maybe
with efficiency) then rewrite the code.

BTW You used the FTSE ;-)


Best wishes
 
M

Marco Wahl

Rafał Maj Raf256 said:

I fear this goes off-topic.

Try a search engine. (Fundamental theorem of software
engineering, Butler Lampson, Andrew Koenig,
indirection)


Best wishes
 
I

iftekhar

I guess The reason you are trying to create this windExt class is
because you want to access some protected members or functions, and
only add new functionality and not member vars. In this case you do not
need to inherit from the windExt class. consider this
class myFrame : public virtual frame {
public:
void Bar() {
const windExt* wex = (const windExt*)this; // since this is a valid
wind* also
wex->Foo();
in2::Show();
}

i have done this a lot of times (borland[win] & gcc 3.2 [linux]),
though i did not check for portability.
Currently I have a workaround: I just wrote simple extend class
(windExt), that do NOT inherit from wind, but only holds a pointer to it.

that means you are calling public functions of wind, right? then why do
you need the windExt class anyway, you can just create a simple
function. please correct me if i am wrong.


best wishes
Iftekhar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top