Computer Science Canada Abstract classes calling abstract methods |
Author: | Raknarg [ Wed Mar 11, 2015 11:26 am ] | ||||
Post subject: | Abstract classes calling abstract methods | ||||
This results as an error on the aforementioned line : "Pirate.o:Pirate.cc:(.text+0x23): undefined reference to `Pirate::generateSpace()'" In java, you can do stuff like this. Is there a way for me to be able to call a pure virtual function in my abstract class? |
Author: | Zren [ Wed Mar 11, 2015 12:59 pm ] | ||
Post subject: | RE:Abstract classes calling abstract methods | ||
Gonna take a guess, but does defining an actual function instead of assigning it as 0 work? It feels like you're assigning a pointer value of 0, possibly to the function's body, which is causing it hiccup. Eg (I have no idea if this is correct syntax):
Also, there's the inline keyword which I noticed in a project I contribute to, though their inline functions weren't virtual. |
Author: | Dreadnought [ Wed Mar 11, 2015 2:48 pm ] |
Post subject: | Re: Abstract classes calling abstract methods |
I believe the problem is that the parent class' constructor is called before the child object's constructor is, making it unclear which subclass' version of the virtual function to call. Suppose AngryPirate is a subclass of Pirate, then creating a new AngryPirate will first create the Pirate part of the object and then create the AngryPirate part. If my guess is correct, your virtual function, which is being called during the Pirate's constructor, doesn't know that it's supposed to call the AngryPirate constructor, because the object is still only a Pirate. Better explanation: http://stackoverflow.com/questions/3091833/calling-member-functions-from-a-constructor Basically, you shouldn't call virtual functions from the constructor, instead just create different constructors for the subclasses. edit: spelling/explanation |