Thread: My method disappeared!!

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    10

    Unhappy My method disappeared!!

    What happends here, and how do i get around it?

    Code:
    class Base
    {
    public:
    	int foo(int, int)
    	{
    		//implemented
    	}
    	virtual int foo(int) = 0;
    };
    
    class Concrete: public Base
    {
    public:
    	virtual int foo(int)
    	{
    		//implemented
    	}
    };
    Creating a Concrete object my compiler does not resolve the
    foo(int,int) method.
    I get the following error using VS.net 2003.

    Code:
    foo' : function does not take 2 arguments
    I guess i could solve it doing something like
    Code:
    class Base
    {
    public:
    	virtual int foo(int, int)
    	{
    		//implemented
    	}
    	virtual int foo(int) = 0;
    };
    
    class Concrete: public Base
    {
    public:
    	virtual int foo(int)
    	{
    		//implemented
    	}
    	virtual int foo(int x,int y)
    	{
    		return Base::foo(x,y);
    	}
    };
    But i think that should not be necessary?
    Help!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    That's the way it is. A function with the same name as a base function, hides the base function--even if it has a different signature. To overload a function, the functions must be declared in the same scope.

    As for the reason, this is from google groups:

    It is to avoid confusion caused by unexpected implicit conversion of
    parameter types, causing a call of an unexpected and sometimes unknown
    method from the base class. (i.e, if only public class interface is
    known, but there are also some unknown, protected methods as well).
    Last edited by 7stud; 11-09-2005 at 06:48 PM.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    10
    =(
    Thanks for the reply 7stud.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You don't have to write a forwarding function in Derived, you can employ a using declaration in Derived to unhide any version of foo in Base that isn't overridden and bring it into Derived's scope:

    using Base::foo;

    I found I had to put that as the first line in Derived's public section to get it to work. Check out this discussion:

    http://groups.google.com/group/comp....&start=2&num=3
    Last edited by 7stud; 11-10-2005 at 04:56 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Arrrgh! I had my Base's and Derived's mixed up, so I had to edit my previous post.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    10
    Aah, that discussion thread cleared it up.
    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM