Newbie inheritance question

Discussion in 'C++' started by keith@bytebrothers.co.uk, Jul 19, 2007.

  1. Guest

    Hi all, I'm getting confused again, so someone please take pity and
    explain. I've got something like this:

    class Base
    {
    void do_work(string&, string&, int&);
    };

    class Derived : public Base
    {
    void do_work(char*, char*, int&);
    };

    int main()
    {
    Derived MyObj;
    string sString, bString;

    MyObj.do_work(aString, bString, 123);
    }

    This gives a compile-time error that basically says

    no matching function for call to `Derived::do_work(string&, string&,
    int)'
    candidates are: void Derived::do_work(char*, char*, int&)

    Why does the compiler not use the matching function from the Base
    class? I can work around it by doing something like this:

    int main()
    {
    Base* ptr = new Derived();
    string aString, bString;

    ptr->do_work(aString, bString, 123);
    delete ptr;
    }

    but that seems to sort of defeat the purpose of inheritance, to my
    newbie eyes at least.
     
    , Jul 19, 2007
    #1
    1. Advertising

  2. Tim Love Guest

    writes:

    >Hi all, I'm getting confused again, so someone please take pity and
    >explain.


    > ...


    >Why does the compiler not use the matching function from the Base
    >class?


    It's because of the way the look-up mechanism works. I was getting
    surprised by this sort of stuff too, so I put together

    http://www-h.eng.cam.ac.uk/help/tpl/languages/C /lookup.html

    which (about half way through) has an example of your situation,
    then eventually tries to supply some reasons.
     
    Tim Love, Jul 19, 2007
    #2
    1. Advertising

  3. xucaen Guest

    On Jul 19, 5:07 am, wrote:
    > Hi all, I'm getting confused again, so someone please take pity and
    > explain. I've got something like this:
    >
    > class Base
    > {
    > void do_work(string&, string&, int&);
    >
    > };
    >
    > class Derived : public Base
    > {
    > void do_work(char*, char*, int&);
    >
    > };
    >
    > int main()
    > {
    > Derived MyObj;
    > string sString, bString;
    >
    > MyObj.do_work(aString, bString, 123);
    >
    > }
    >
    > This gives a compile-time error that basically says
    >
    > no matching function for call to `Derived::do_work(string&, string&,
    > int)'
    > candidates are: void Derived::do_work(char*, char*, int&)
    >
    > Why does the compiler not use the matching function from the Base
    > class? I can work around it by doing something like this:
    >
    > int main()
    > {
    > Base* ptr = new Derived();
    > string aString, bString;
    >
    > ptr->do_work(aString, bString, 123);
    > delete ptr;
    >
    > }
    >
    > but that seems to sort of defeat the purpose of inheritance, to my
    > newbie eyes at least.



    I guess the short answer is that the Derived::do_work() method hides
    the base class method. You will need to either a) add both
    declarations in the Base class, or b) change the name of the Derived
    class method, or c) add both declarations to the Derived class. My
    example below implements options c.
    Something else I want to add is that the third parameter is a
    reference to an int, so you will need to pass in a variable, not a
    constant. I get a compile error when I try passing the constant 123.


    class Base
    {
    public:
    virtual void do_work(string&, string&, int&);

    };

    class Derived : public virtual Base
    {
    public:
    void do_work(string&, string&, int&);
    void do_work(char*, char*, int&);

    };
    void Derived::do_work(string& a, string& b, int& c)
    {
    cout << a << ", " << b << ", " << c << endl;
    }
    void int main()
    {
    Derived MyObj;
    string aString, bString;
    int myInt = 123;

    MyObj.do_work(aString, bString, myInt);

    }

    Jim
     
    xucaen, Jul 19, 2007
    #3
  4. duane Guest


    > I guess the short answer is that the Derived::do_work() method hides
    > the base class method. You will need to either a) add both
    > declarations in the Base class, or b) change the name of the Derived
    > class method, or c) add both declarations to the Derived class. My
    > example below implements options c.
    > Something else I want to add is that the third parameter is a
    > reference to an int, so you will need to pass in a variable, not a
    > constant. I get a compile error when I try passing the constant 123.
    >
    >
    > class Base
    > {
    > public:
    > virtual void do_work(string&, string&, int&);
    >
    > };
    >
    > class Derived : public virtual Base
    > {
    > public:
    > void do_work(string&, string&, int&);
    > void do_work(char*, char*, int&);
    >
    > };


    Wouldn't a using clause work here as well:

    class Derived : public virtual Base
    {
    using Base::do_work;
    public:
    void do_work(char*,char*,int&);
    };

    At least this should bring it into scope and allow
    an overload?
     
    duane, Jul 19, 2007
    #4
  5. Kai-Uwe Bux Guest

    wrote:

    > Hi all, I'm getting confused again, so someone please take pity and
    > explain. I've got something like this:
    >
    > class Base
    > {
    > void do_work(string&, string&, int&);
    > };
    >
    > class Derived : public Base
    > {
    > void do_work(char*, char*, int&);
    > };
    >
    > int main()
    > {
    > Derived MyObj;
    > string sString, bString;
    >
    > MyObj.do_work(aString, bString, 123);
    > }
    >
    > This gives a compile-time error that basically says
    >
    > no matching function for call to `Derived::do_work(string&, string&,
    > int)'
    > candidates are: void Derived::do_work(char*, char*, int&)


    Correct.

    > Why does the compiler not use the matching function from the Base
    > class?


    Uhm, there is no matching function in the base class. Try to compile this:

    #include <string>

    using std::string;

    class Base
    {
    void do_work(string&, string&, int&);
    };

    int main()
    {
    Base MyObj;
    string aString, bString;

    MyObj.do_work(aString, bString, 123);
    }

    You will find that the error is still there. The problem is the parameter
    123, which will not be used to bind to an int& parameter.


    > I can work around it by doing something like this:
    >
    > int main()
    > {
    > Base* ptr = new Derived();
    > string aString, bString;
    >
    > ptr->do_work(aString, bString, 123);
    > delete ptr;
    > }


    Apparently, you did not try this code. You would have found that the
    compiler still complains. Please post the actual code that gives you
    problems.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jul 19, 2007
    #5
  6. sumedh..... Guest

    On Jul 19, 2:07 pm, wrote:
    > Hi all, I'm getting confused again, so someone please take pity and
    > explain. I've got something like this:
    >
    > class Base
    > {
    > void do_work(string&, string&, int&);
    >
    > };
    >
    > class Derived : public Base
    > {
    > void do_work(char*, char*, int&);
    >
    > };
    >
    > int main()
    > {
    > Derived MyObj;
    > string sString, bString;
    >
    > MyObj.do_work(aString, bString, 123);
    >
    > }
    >
    > This gives a compile-time error that basically says
    >
    > no matching function for call to `Derived::do_work(string&, string&,
    > int)'
    > candidates are: void Derived::do_work(char*, char*, int&)
    >
    > Why does the compiler not use the matching function from the Base
    > class? I can work around it by doing something like this:
    >
    > int main()
    > {
    > Base* ptr = new Derived();
    > string aString, bString;
    >
    > ptr->do_work(aString, bString, 123);
    > delete ptr;
    >
    > }
    >
    > but that seems to sort of defeat the purpose of inheritance, to my
    > newbie eyes at least.


    well if you want to do anything like this which is function
    overriding... u need to have exactly same fucntions in base and
    dervied class. Well i think this serves ur purpose....
    Cheers,
    Sumedh
     
    sumedh....., Jul 20, 2007
    #6
    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. maxw_cc
    Replies:
    1
    Views:
    3,160
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    833
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,055
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,928
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    783
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page