Having trouble overriding base class's function from subclass
I have an actor component InteractionBase, some child of which goes on every object you can interact with in the world. InteractionBase has an empty Use function, declared thusly:
My intent is to make a bunch of subclasses and override Use in a different way in each class, such that talking to a person, pressing a switch, and picking up loot can all be accomplished by calling that object's InteractionSomeChild->Use(myActor); .
To test the functionality, I made a subclass of InteractionBase named InteractionSpeech and included the following declaration in the header:
To the best of my knowledge, this should let me add whatever functionality I wish, but it produces three linker errors that I'm having trouble deciphering:
Is there an obvious klutz-up with how I declared my override?
asked Aug 19 '15 at 05:21 AM in C++ Programming
To be able to override a function, that function must be declared as virtual in the parent class, not the overriding child class. To make sure, that you are actually overriding a function, you can use the "override" keyword, like so:
The "virtual" in the Child class is optional, since the function being virtual is inherited from the parent class. However it is considered good practice to re-declare the virtual, to make it clear, that this function is indeed virtual (even though that would be evident from the override-keyword anyway).
The "override"-keyword tells the compiler to throw a compile-time-error if the function does not actually override a parent function, and also clearly communicates the programmer's intent, that he wants to override a parent function.
One last thing: It seems you get a Linker Error on UInteractionSpeech:Use. UInteractionSpeech is probably the parent class. You need to provide an implementation of the parent class's method, even if it's just empty or with an error-log-statement:
answered Aug 19 '15 at 07:02 AM
since when does one need to add override ?wtf ?
answered Mar 18 '18 at 10:22 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here