# Why does this work?

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

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

##### Share on other sites
mucho gracias senior.

##### Share on other sites
Quote:
 Original post by MaulingMonkeyBecause the =0 indicates that the function is a PURE VIRTUAL

For a second I thought you said PURE EVIL....

##### 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 on other sites
Doc: lol. I didn't say Micro\$oft ;-).
hplus0603: good point. I gotta break the habbit of saying "can't"/"never" ;-).

##### Share on other sites
Quote:
 Original post by PnP Biosmucho gracias senior.

*Puts Nazi helmet*

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

##### Share on other sites
Quote:
 Original post by hplus0603A 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?

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633417
• Total Posts
3011781
• ### Who's Online (See full list)

There are no registered users currently online

×