Sign in to follow this  
PnP Bios

Why does this work?

Recommended Posts

//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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this