Connecting Tech Pros Worldwide Forums | Help | Site Map

[lame] overloaded fun. virt. and inherity

Rafal 'Raf256' Maj
Guest
 
Posts: n/a
#1: Jul 19 '05

Hi,
I have base class that defines function 'fun'. This function is overloaded
- it can be used fr argument int or for const char* Function is virtual.

Now I create a dervied class cB, and I create new definitions of both
cB::fun - cB::fun(int) and cB::fun(const char*) - all is o.k.

but - now I decided that function cB::fun(const char*) is same as from
cA, so I change it's body to :

cB::fun(const char* s) { cA::fun(s); }

(all is stil ok) - and finaly I commented out the cB::fun(const char* s)
function, so that inherit mechanizm can take care of it. But then -
compiler acts like thar is no cB::fun(const char* s);

problem when using this classes :


class cA {
public:
cA(){};
virtual void fun(int v) { ... }
virtual void fun(const char* v) { ... }
};
class cB : public cA {
public:
cB(){};
virtual void fun(int v) { gConsole->Print("cB-int\n"); }
// !!! virtual void fun(const char* v) { ... }
void xxx() { ... }
};


cB *p = new cB;
p->xxx(); // ok
p->fun(3); // ok
cA->fun("a"); // compiler error - it doesn't see cB::fun(const char*)
// ant therefore it tries to convert const char* into 'int' and call
// cB::fun(int), instead of calling inherited cA::fun(const char*)

((cA*)p)->fun("a"); // this do work "manualy"

cB b;
b.fun("a"); // same compiler error


currently I use a workaround

void cB::fun(const char* v) { cA::fun(v); }

so i'm manualy calling cA's version of fun() instead of just inheriting it
but it's only a (bad) workaround





--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~l-.~~~~~~~~~~~~~~~~~~~
GG-1175498 ____| ]____,
Rafal 'Raf256' Maj X-( * )
Rafal(at)Raf256(dot)com ,"----------"

Ron Natalie
Guest
 
Posts: n/a
#2: Jul 19 '05

re: [lame] overloaded fun. virt. and inherity



"Rafal 'Raf256' Maj" <spam@raf256.com> wrote in message news:Xns9414B3085A73raf256com@213.180.128.20...[color=blue]
> (all is stil ok) - and finaly I commented out the cB::fun(const char* s)
> function, so that inherit mechanizm can take care of it. But then -
> compiler acts like thar is no cB::fun(const char* s);
>[/color]

You don't understand inheritance. Only the name is inheritted. None
of the cA::fun definitions appear in cB because the cB::fun definition
hides them.

Add
using cA::fun;
to cB to bring forward the definitions from there.
[color=blue]
> cA->fun("a"); // compiler error - it doesn't see cB::fun(const char*)
> // ant therefore it tries to convert const char* into 'int' and call
> // cB::fun(int), instead of calling inherited cA::fun(const char*)[/color]

It doesn't see cB::fun because there is no cB fun.

Non-static member calling works like this:

1. The name is looked up (yielding cB::fun)
2. Possible overloads for the name are considered (there is only one cB::fun(int))
3. Access is checked (ok, public)
4. Virtual substitution occurs.


red floyd
Guest
 
Posts: n/a
#3: Jul 19 '05

re: [lame] overloaded fun. virt. and inherity


Rafal 'Raf256' Maj wrote:
[color=blue]
> Hi,
> I have base class that defines function 'fun'. This function is overloaded
> - it can be used fr argument int or for const char* Function is virtual.
>
> Now I create a dervied class cB, and I create new definitions of both
> cB::fun - cB::fun(int) and cB::fun(const char*) - all is o.k.
>
> but - now I decided that function cB::fun(const char*) is same as from
> cA, so I change it's body to :
>
> cB::fun(const char* s) { cA::fun(s); }
>
> (all is stil ok) - and finaly I commented out the cB::fun(const char* s)
> function, so that inherit mechanizm can take care of it. But then -
> compiler acts like thar is no cB::fun(const char* s);
>
> problem when using this classes :
>
>
> class cA {
> public:
> cA(){};
> virtual void fun(int v) { ... }
> virtual void fun(const char* v) { ... }
> };
> class cB : public cA {
> public:
> cB(){};[/color]

using cA::fun;
[color=blue]
> virtual void fun(int v) { gConsole->Print("cB-int\n"); }
> // !!! virtual void fun(const char* v) { ... }
> void xxx() { ... }
> };
>[/color]
=[color=blue]
>[/color]

Closed Thread