 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
varun sharma Guest
|
Posted: Fri Jul 16, 2004 11:13 am Post subject: Overriding baseclasses |
|
|
Hi,
I am facing a problem when overriding a function derived from two
different baseclasses. To be clear, i am having the derived class Z,
derived from X & Y.
Both in X & Y there is a function, say func1(). I want to override
this function in my class Z. The function is having different return
types in the baseclasses. I want the function with the one in X.
How can i declare it in my derived class so that i can have it
overridden from the class X.
Thanks in Advance
Varun
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
spuds Guest
|
Posted: Fri Jul 16, 2004 1:34 pm Post subject: Re: Overriding baseclasses |
|
|
use the scope resolution operator as follows:
when you have an ambiguous function name you must explicitly specify which
one you are calling
using namespace std;
class base1
{
public:
base1() {;}
int commonBaseFunction() { return 7; };
};
class base2
{
public:
base2() { ; }
float commonBaseFunction() { return 7.9; };
};
class derived : public base1, public base2
{
public:
derived() { ; }
int getIntNumber() { int i = base1::commonBaseFunction();
return i;
}
float getFloatNumber() { float f = base2::commonBaseFunction();
return f;
}
};
int main()
{
float f, df;
int i, di;
derived d;
i = d.base1::commonBaseFunction();
f = d.base2::commonBaseFunction();
cout << i << " " << f << endl;
di = d.getIntNumber();
df = d.getFloatNumber();
cout << di << " " << df << endl;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Jul 17, 2004 9:59 am Post subject: Re: Overriding baseclasses |
|
|
varun sharma wrote:
| Quote: | I am facing a problem when overriding a function derived from two
different baseclasses. To be clear, i am having the derived class Z,
derived from X & Y.
Both in X & Y there is a function, say func1(). I want to override
this function in my class Z. The function is having different return
types in the baseclasses. I want the function with the one in X.
How can i declare it in my derived class so that i can have it
overridden from the class X.
|
Don't derive from Y, split the Y into two parts and derive from the
part that you are going to use. Apparently, you're not interested
in Y::func1.
Since you're talking "overriding", the functions must be virtual.
If they both have the same argument number and types, no matter which
one you try to override, you'll [try to] override _both_, since the
language only makes the distinction by the name and argument types.
struct X {
virtual int func1();
};
struct Y {
virtual char func1();
};
struct Z : X, Y {
int func1(); // error -- Z::func1overrides Y::func1,
// but differs in return type.
};
The only solution here is not to derive from Y, AFAIK.
Victor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Jul 20, 2004 9:41 pm Post subject: Re: Overriding baseclasses |
|
|
| Quote: | varun sharma wrote:
I am facing a problem when overriding a function derived from two
different baseclasses. To be clear, i am having the derived class Z,
derived from X & Y.
Both in X & Y there is a function, say func1(). I want to override
this function in my class Z. The function is having different return
types in the baseclasses. I want the function with the one in X.
How can i declare it in my derived class so that i can have it
overridden from the class X.
|
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | Don't derive from Y, split the Y into two parts and derive from the
part that you are going to use. Apparently, you're not interested
in Y::func1.
Since you're talking "overriding", the functions must be virtual.
If they both have the same argument number and types, no matter which
one you try to override, you'll [try to] override _both_, since the
language only makes the distinction by the name and argument types.
struct X {
virtual int func1();
};
struct Y {
virtual char func1();
};
struct Z : X, Y {
int func1(); // error -- Z::func1overrides Y::func1,
// but differs in return type.
};
The only solution here is not to derive from Y, AFAIK.
|
struct XX : public X {
virtual int X_func1() { return X::func1(); }
};
struct YY : public Y {
virtual int Y_func1() { return Y::func1(); }
};
struct Z : public XX, public YY {
int X_func1() { ... };
};
int main() {
Z z;
z.X_func1(); // Calls Z::X_func1(), which is overridden
z.Y_func1(); // Calls YY::Y_func1(), which calls Y::func1()
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|