Derived class doesn't see base class function

This is a discussion on Derived class doesn't see base class function within the C++ Programming forums, part of the General Programming Boards category; I have a case where a derived class cannot see a virtual function defined in a public base class. The ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Derived class doesn't see base class function

    I have a case where a derived class cannot see a virtual function defined in a public base class. The only explanation I can come up with is that C++ must have some kind of rule that says if you override a base class function with name "X", then you must override all base class functions having the same name. Is this a rule? See the following example:

    Code:
    class base
    {
    public:
      virtual int func();
      virtual void func(int arg1,int arg2);	
      virtual void func2(int arg1,int arg2) { func(arg1,arg2); }
    };
    
    class derived : public base
    {
    public:
      // override func(), but not func(arg1,arg2)
      virtual int func();
    };
    
    void test
    {
      derived xyz;
      int i = xyz.func();  // compiles OK
      xyz.func(1,2);           // compilation error: "func() does not take two arguments"
      xyz.func2(1,2); // compiles OK
    };
    Last edited by ejubenville; 11-11-2009 at 12:10 PM. Reason: coding error in example

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Quote Originally Posted by ejubenville
    The only explanation I can come up with is that C++ must have some kind of rule that says if you override a base class function with name "X", then you must override all base class functions having the same name. Is this a rule?
    Not quite. The rule is that a member name in the derived class hides members of the same name in the base class. You can bring in those hidden names by using a using declaration:
    Code:
    class derived : public base
    {
    public:
      using base::func;
      // override func(), but not func(arg1,arg2)
      virtual int func();
    };
    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

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Problem solved

    Thanks!

    The "using" declaration fixed the hidden names!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 06:23 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

Tags for this Thread


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