[lame] overloaded fun. virt. and inherity

R

Rafal 'Raf256' Maj

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
 
R

Ron Natalie

Rafal 'Raf256' Maj said:
(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);

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.
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*)

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.
 
R

red floyd

Rafal said:
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(){};

using cA::fun;
 

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

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top