Thread: function overloading problem

  1. #1
    AndersSundman
    Guest

    Question function overloading problem

    Hi,

    I have encountered a problem, perhaps someone knows how to fix this..

    In a base class A I have two virtual member functions, one is implemented (f1), the other one abstract (f2).

    In a second class B, that inherits from A I wan't to implement the second function (f2). It looks something like (simplified):

    class A {
    public:
    virtual f1();

    protected:
    virtual f2() = 0;
    };

    class B : public A {
    protected:
    virtual f2();
    };

    The problem is that the compiler gives me the error message:
    "f2 hides virtual function f1", when I compile the B class.

    What to do.. Do I have to redeclare the f1 function in the B class, in that case how do I do that?

    - Anders

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Do I have to redeclare the f1 function in the B class, in that case how do I do that?

    No there's either another problem with your code, or compiler. This works -

    Code:
    #include <iostream>
    
    using namespace std;
    
    class A { 
    public: 
    	virtual void f1(){cout << "f1";} 
    
    protected: 
    	virtual void f2() = 0; 
    }; 
    
    class B : public A { 
    protected: 
    	void f2(){cout << "f2";} 
    }; 
    
    int main (){ 
    
    	B t;
    	t.f1();
    	return 0;
    }

  3. #3
    AndersSundman
    Guest
    woops I seem to have missed out the most important part, when I was simplifying my example: the functions f1 & f2 have the same name.. they are the () operator but they take different argument types.. So I guess it accualy looks more like:

    class A {
    public:
    virtual void operator() (arg1) {}

    protected:
    virtual void operator() (arg2) = 0;
    };

    class B : public A {
    protected:
    void operator() (arg2) {}
    };

    and this doesn't work.. sorry about that..

    - Anders S

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    would a template be applicable to this situation. It sounds like the easiest way.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You can't overload derived functions and still expect the base versions to be called. Inheritance works by nesting the base class within your derived class, so the compiler won't check the inner functions unless it finds no match in the outer. The compiler will find what it thinks is a match in the outer scope and will either try and call that one or issue an error if the parameters don't match.

    You'll have to re-declare in the derived class, so you may aswell make both methods pure in the abstract base. -

    Code:
    #include <iostream>
    
    using namespace std;
    
    class A { 
    public: 
    	virtual void operator()(float a)=0; 
    
    protected: 
    	virtual void operator()(int b) = 0; 
    }; 
    
    class B : public A {
    public:
    	void operator ()(float a){cout << a;}
    protected: 
    	void operator ()(int b){} 
    	
    }; 
    
    int main (){ 
    
    	B t;
    	t(2.0f);
    	return 0;
    }

  6. #6
    AndersSundman
    Guest

    Talking

    Wopie!!! It works....

    thanks a bunch...

    I can't realy implement the first funtion in the derived class.. since I have a bunch of derived classes, but if I just redeclare it in B as you said :

    operator() (arg1) {
    A:perator()(arg1);
    }

    It works like a dream..

    Milions of thanks..

    - Anders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:28 AM
  4. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM