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 11:10 AM. Reason: coding error in example

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,623
    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();
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06: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