[lame] overloaded fun. virt. and inherity

Discussion in 'C++' started by Rafal 'Raf256' Maj, Oct 14, 2003.

  1. 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 ,"----------"
     
    Rafal 'Raf256' Maj, Oct 14, 2003
    #1
    1. Advertising

  2. Rafal 'Raf256' Maj

    Ron Natalie Guest

    "Rafal 'Raf256' Maj" <> wrote in message news:Xns9414B3085A73raf256com@213.180.128.20...
    > (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.
     
    Ron Natalie, Oct 14, 2003
    #2
    1. Advertising

  3. Rafal 'Raf256' Maj

    red floyd Guest

    Rafal 'Raf256' Maj wrote:

    > 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;

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

    =
    >
     
    red floyd, Oct 14, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vijay
    Replies:
    6
    Views:
    406
    vijay
    Aug 22, 2003
  2. er
    Replies:
    2
    Views:
    509
  3. Inconnu
    Replies:
    0
    Views:
    207
    Inconnu
    Jun 22, 2011
  4. Inconnu
    Replies:
    4
    Views:
    347
    Johannes Schaub (litb)
    Jun 23, 2011
  5. Roy79
    Replies:
    1
    Views:
    123
    Pandurang Nayak
    Mar 22, 2006
Loading...

Share This Page