Why does this work?

Started by
6 comments, last by Zahlman 19 years, 7 months ago

//base interface class
//
class cStateInterface
{
public:
	//cStateInterface(void);
	//~cStateInterface(void);

	virtual void Startup(SDL_Surface *renderTarget) = 0;
	virtual void Shutdown(void) = 0;

	virtual void Init(void) = 0;

	virtual void Render(void) = 0;
	virtual int Process(SDL_Event event, float time) = 0;

private:
};

why do I have to set the members = 0 in order for this to link right. It compiles fine with out the '= 0'. I don't even remember where I got the = 0 from. but, oddly enough, it works just fine.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
Advertisement
Quote:Original post by PnP Bios
*** Source Snippet Removed ***

why do I have to set the members = 0 in order for this to link right. It compiles fine with out the '= 0'. I don't even remember where I got the = 0 from.

but, oddly enough, it works just fine.


Because the =0 indicates that the function is a PURE VIRTUAL, meaning for that base class, it dosn't actually exist, and you are also then not allowed to create instances of that class. You can, however, have pointers to this class (which would really be assigned classes that derive from that) and call those functions (which would really call the derived classes implementatations of that function).

By removing the = 0 you're telling it that it's VIRTUAL, but NOT A PURE VIRTUAL, meaning it can be overridden in a derived class, but IT HAS A BODY (for if it isn't, or if you create an instance of the base class).

It compiles just fine because it dosn't need the body to know how to create an object file that says somewhere in it "I want to call function X".

The link fails because it can't change the command from "call function X" to "jump to this address" because there's no body (and thus no address) for it to set it to jump to.
mucho gracias senior.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
Quote:Original post by MaulingMonkey
Because the =0 indicates that the function is a PURE VIRTUAL


For a second I thought you said PURE EVIL....
My stuff.Shameless promotion: FreePop: The GPL god-sim.
A pure virtual CAN have a body (try it -- it works!) but it doesn't HAVE to have a body -- and it HAS to be overridden in a concrete class to be instantiable.

Non-pure virtuals HAVE to have a body, as they will get referenced by the vtable; if the reference is not satisfied, the linker will be sad.
enum Bool { True, False, FileNotFound };
PnP: De nada.
Doc: lol. I didn't say Micro$oft ;-).
hplus0603: good point. I gotta break the habbit of saying "can't"/"never" ;-).
Quote:Original post by PnP Bios
mucho gracias senior.


*Puts Nazi helmet*

It's "muchas" gracias, señor (ñ=ALT-[164])
[size="2"]I like the Walrus best.
Quote:Original post by hplus0603
A pure virtual CAN have a body (try it -- it works!) but it doesn't HAVE to have a body -- and it HAS to be overridden in a concrete class to be instantiable.

Non-pure virtuals HAVE to have a body, as they will get referenced by the vtable; if the reference is not satisfied, the linker will be sad.


o_O They didn't teach me quite that much about it in university.

class Foo {  virtual void wibble() = 0;}void Foo::wibble() {  // implementation}class Bar : public Foo {  void wibble();}void Bar::wibble() {  // different implementation}// Do you mean that Foo is now instantiable, or just Bar?// And if Foo is not instantiable, how do you access // Foo::wibble()?// More imporantly, why would you ever want to do this?

This topic is closed to new replies.

Advertisement