funcs with same names - in base and derived classes

This is a discussion on funcs with same names - in base and derived classes within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; class base { public: void foo() { cout << "base" << endl; ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    funcs with same names - in base and derived classes

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class base
    {
    public:
    	void foo() { cout << "base" << endl; }
    };
    
    class derived : public base
    {
    public:
    	void foo(const string& msg) { cout << "derived: " << msg << endl; }
    };
    
    int main()
    {
    	derived d;
    	d.foo();
    
    	return 0;
    }
    the code produces following error:
    error C2660: 'derived::foo' : function does not take 0 arguments
    and it seems very silly to me why just cant the compiler use the base class version?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    sorry, its been so long since ive used OOP with C++.
    a quick fix is to cast d to base, ie: ((base) d).foo();, but that kind of defeats the purpose of polymorphism.

    search around for class design and function overloading until someone can give you a more accurate answer. maybe it has to be something to do with virtual?
    Last edited by nadroj; 11-30-2007 at 12:05 AM.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    duh!
    i had to do this in derived class, and it works now!
    Code:
    class derived : public base
    {
    public:
    // this solves the problem
    	using base::foo;
    	void foo(const string& msg) { cout << "derived: " << msg << endl; }
    };
    and i am not very happy about this although it works fine now!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    The reason is that you aren't overriding the base class foo function there, you're actually trying to overload it. However the overloads in 'base' aren't being considered in calls to foo for 'derived' objects.
    Adding:
    Code:
    using base::foo;
    inside the definition of 'derived' will make the 'base' class overload visible to 'derived'.
    At least that's my understanding. I could be wrong.

    Edit: Doh, a fraction too slow.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    you are right!
    that's what i found after a bit searching.
    see previous post.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    So, all happy now, right? Not looking for something like virtual functions, are you?

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Elysia, virtual functions? Hmmmm ....
    Actually the function in base class was virtual earlier.
    And I suspected it to be the cause of problem and removed virtual from the definition.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    Virtual functions only work as they should if you have two identical functions in two or more classes, each derived from each other, or from a base class.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, this is called "hiding" - if you implement a function with the same name in the derived class, but with different "signature" [different return type or arguments], then the base class function is "hidden" from the derived class. You can, as shown, "unhide" it by explicitly tell the compiler that you want to use this function, with
    Code:
    using base::foo;
    But it's a bad idea in general - it's better to make it clearer that they are different functions by having different names, particularly if you want to use both.

    Or implement both functions in the base-class - maybe with one of the forms using a "pure virtual" setting, if that's what makes more sense.

    It really depends on what you are doing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    As shown in my example (well not exactly the same but you got the point I hope).
    There was a situation during a project ...
    The base class function would take no arguments and would do the same
    that a derived class function was also doing (but required arguments, that I did not have) !
    The problem was that I only had access to an initialized object that was derived.
    And i thought I could access the base function using derived object but ...
    As shown in my post it does not work unless you change the derived class source.
    Which I had chosen not to do in my project

    As nadroj suggested. Would a typecast be clean solution to the problem?
    Last edited by manav; 11-30-2007 at 03:09 AM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    If the functions are different, why not just cast it to the base object and call the function?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    If the functions are different, why not just cast it to the base object and call the function?
    Using a different name would be better. Having to explicitly cast an object of a derived class to an object of its base class in order to invoke a member function means that the hierarchy does not conform to the Liskov substitution principle.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    I agree, but I assume in this example, that the OP cannot change the derived class and thus needs to find another way.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if you can't change the base class, and also not change the derived class, what's going on? It's a badly designed class in the first place if it hides the base's methods.

    Edit: Yes, I'm well aware that sometimes you can't change someone elses class, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    If you can modify the class(es), then do so. Otherwise, you may have to do a typecast, yes.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Virtual Base Classes
    By skewray in forum C++ Programming
    Replies: 11
    Last Post: 12-21-2006, 05:56 PM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21