I have a subclass of a class named Entity, and I wan’t that subclass to override Entity’s constructor, but calling Entity’s constructor too. So I declare the constructor like this:
EntitySphere(GLuint shader):Entity(shader){
//Code
}
But that method only works if I implement the function there, in the class definition (or I get a lot of errors). How can I achieve the same, but being able to implement the constructor in the .cpp file?
To implement a constructor (or any method, for that matter) outside the class definition, you need to specify the name of the class before the method name:
Notice the
EntitySphere::part before the rest of the method.Otherwise, the compiler doesn’t know which method you’re providing the definition for. (Maybe it could be allowed to guess, but what if it guessed wrong and associated that implementation with some other function in your program?)