Thread: class virtual function inheritance

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    class virtual function inheritance

    Is there a way not to declare a virtually declared function in a superclass? Or is it mandatory? Namely:

    Code:
    class A
    {
    public:
    	virtual void some_function(arglist);
    };
    
    class B : public A
    {
    public:
    	B(){whatever;}
    };
    
    void B::some_function(arglist)
    {
    	yadayada;
    }
    My compiler won't allow me to do it, so I'm declaring them again. So, is there a way out?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You need to add "void some_function(arglist);" to the class B definition.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I know you have to do that. I was just wondering if there was a way to get away from doing it.

    With my puny understanding, I really can't see the need of having to declare a virtual function everytime in some subclass.

    You have a prototype declared in some ueber-superclass. And since it's virtual, even if you override in some subclass without declaring it again in the class body, wouldn't the compiler know to look in the terminal subclass? Or even in an intermediary subclass?

    But since it isn't so, and I'm willing to bet my entire life savings that the people on the c++ standard committee know better than I do, I guess there is a good reason that it ain't so.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Is there a way not to declare a virtually declared function in a superclass?
    Sure. But here is the difference:
    Code:
    #include <iostream>
    using namespace std;
    
    class Dog
    {
    public:
    	void identify()
    	{
    		cout<<"I'm a dog."<<endl;
    	}
    };
    
    class GermanShepherd : public Dog
    {
    	void identify()
    	{
    		cout<<"I'm a German Shepherd."<<endl;
    	}
    };
    
    int main()
    {
    	Dog* pDog = 0;
    
    	Dog aDog;
    	pDog = &aDog;
    	pDog->identify();   //I'm a dog.
    
    	GermanShepherd Max;
    	pDog = &Max;
    
    	pDog->identify();  //I'm a dog.
    
    	return 0;
    }
    Code:
    #include <iostream>
    using namespace std;
    
    class Dog
    {
    public:
    	virtual void identify()
    	{
    		cout<<"I'm a dog."<<endl;
    	}
    };
    
    class GermanShepherd : public Dog
    {
    	void identify()
    	{
    		cout<<"I'm a German Shepherd."<<endl;
    	}
    };
    	
    
    int main()
    {
    	Dog* pDog = 0;
    
    	Dog aDog;
    	pDog = &aDog;
    	pDog->identify(); //I'm a dog.
    
    	GermanShepherd Max;
    	pDog = &Max;
    
    	pDog->identify();  //I'm a German Shepherd.
    	
    	return 0;
    }
    Last edited by 7stud; 05-12-2006 at 10:30 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I thought you could, but apparently not.
    Thanks for that much needed kick in the head. I don't know what got into me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 07:34 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM