Connecting Tech Pros Worldwide Help | Site Map

[lame] overloaded fun. virt. and inherity

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:12 PM
Rafal 'Raf256' Maj
Guest
 
Posts: n/a
Default [lame] overloaded fun. virt. and inherity


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 ,"----------"

  #2  
Old July 19th, 2005, 07:13 PM
Ron Natalie
Guest
 
Posts: n/a
Default 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.


  #3  
Old July 19th, 2005, 07:13 PM
red floyd
Guest
 
Posts: n/a
Default 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]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.