Jump to content
  • Advertisement
Sign in to follow this  
PnP Bios

Why does this work?

This topic is 5080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share this post


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

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 PnP Bios
mucho gracias senior.


*Puts Nazi helmet*

It's "muchas" gracias, señor (ñ=ALT-[164])

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!