473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

virtual inherite

#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?
Mar 22 '06 #1
8 2038
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
--
Wymiana starych układów... na nowe układy - prawie jak walka z korupcja.
Walka z wychowaniem seksualnym i erotykÄ… - prawie jak walka z patologiÄ….
PiS - prawie jak prawo i sprawiedliwość... Prawie. Prawie robi różnicę.
Myśl. Głosuj rozsądnie. Nie na tanie hasła. // Rafał Maj Raf256
Mar 22 '06 #2
Rafał Maj Raf256 <us*******************@raf256.com.invalid> writes:
#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

Mar 22 '06 #3
Marco Wahl wrote:
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()
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.

--
Wymiana starych układów... na nowe układy - prawie jak walka z korupcja.
Walka z wychowaniem seksualnym i erotykÄ… - prawie jak walka z patologiÄ….
PiS - prawie jak prawo i sprawiedliwość... Prawie. Prawie robi różnicę.
Myśl. Głosuj rozsądnie. Nie na tanie hasła. // Rafał Maj Raf256
Mar 22 '06 #4

"Rafal Maj Raf256" <us*******************@raf256.com.invalid> wrote in
message news:dv**********@inews.gazeta.pl...
#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
Mar 22 '06 #5
> [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

Mar 23 '06 #6
Marco Wahl wrote:
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 ;-)


FTSE?
Mar 23 '06 #7
Rafał Maj Raf256 <us*******************@raf256.com.invalid> writes:
BTW You used the FTSE ;-)


FTSE?


I fear this goes off-topic.

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

Mar 24 '06 #8
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

Mar 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | ov | last post by:
Hi, My application includes a class library which has both managed C++ and unmanaged C++ code. The unmanaged C++ code includes only one class with both virtual methods and non-virtual methods....
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
0
by: miller chen | last post by:
Using WebServices, and server add the new class PersonSet that inherite DataSet,follow down: public class PersonSet:DataSet { private string _strB; public string strB { get
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
by: Ryan Liu | last post by:
Hi, Can someone give me a hlep on inherited user control? I try to inherite windows forms user control, like ListView. From Visual studio 2003, I right click a folder and selct "Add...
4
by: harvie wang | last post by:
Hi, I want to add more buttons to messagebox, such as "Apply All". Can i implement a custom messagebox, inherite from System.Windows.Form.MessageBox? Best wish, harvie 2006-1-18
1
by: jhamw1 | last post by:
Hi I have a simple question, I cant find the solution sofar. I have an aspx page where I want to use my own custom <asp:CustomTextBox>. How do I manage that using C# ? I want to inherite the...
12
by: feel | last post by:
Hi,All I am sure it's an old question. But I just find a interesting design about this: Polymorphism without virtual function in a C++ class. My solution is for some special case, trust me, very...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.