Overriding Inherited Virtual Functions

Is it possible to do something like this:

1
2
3
4
5
6
7
8
9
10
11
class A //parent
{
   public:
      virtual void DoSomething() = 0;
};

class B : public A //child
{
   public:
      void DoSomething(string s) override;
}


Where the child member function overrides and changes the parents member function.

I need to pass an array of key states to the Controller class' Update() function but don't want to send it to every class derived from Object (like Controller).

Is this possible or do I have to overload the original Update() member function (but I would need to define the method in Object then (i.e remove the pure virtual function (=0)))
No. The overridden virtual function cannot have a different signature from the function it overrides. It would be impossible to call.

Think about it:

1
2
3
4
5
6
7
A* a = new B;

a->DoSomething( "foo" ); // error, A::DoSomething doesn't take a string
  // remember that at this point, a may or may not point to a B object.  It might point
  //  to an A or some other type derived from A.

a->DoSomething();  // works, but now you don't have a string passed 
Last edited on
Yeah but I do not need a common interface for my classes. My structure is as follows:

HumanController and AIController

both inherit from Controller

which inherits from Object.

Each needs an update function. The Object class knows nothing about what its updating and the Controller class doesn't know enough about its type to update. However the HumanController needs an array of boolen key info whereas the AIController needs an array of AINodeInfo for path finding.

I could make a function in Object as follows:

virtual void Update(bool keys[], AINodeInfo** ai);

But then that type specific information is being passed to instances that have no need for it.

Is there a way to do what I need or is my structure flawed?
Yeah but I do not need a common interface for my classes.


If you do not need a common interface, then why use virtual functions at all? Providing common interfaces is exactly what they're for.


The real question here is this: Does the code calling update know enough about the object to pass it the keys or the ai?

If it does, then you don't need virtual functions, and Update() can be a normal non-virtual member of HumanController and AIController.

If it does not, then you'll have to provide a common interface that's generic enough to work with both classes.

In the case of the latter, you can probably pass in the keys/ai in a separate (nonvirtual) function, then have a general virtual Update() function that takes 0 parameters. (though this probably is not a great solution)
Last edited on
Thanks, my knowledge in this area is pretty shaky.
Topic archived. No new replies allowed.