tags:

views:

157

answers:

3
struct device{
   virtual void switchon()=0{}
};

int main() 
{ 

} 

I wrote code similar to following and it gave error.

pure-specifier on function-definition compilation terminated due to -Wfatal-errors.

When I asked him, he showed me the following quote from the Standard

A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).

I can't seem to understand what it means and I think this somehow is not relevant. Any help in this is appreciated.

PS: If this is not the relevant quote, pls guide me to the proper one so that I can have a better counterargument.

+1  A: 

I think the problem is that you're declaring a method as pure virtual (using "= 0"), but you are also defining the method by putting the {} at the end. Try removing the curly braces if you really want this to be pure virtual.

By "definition" a pure virtual function should have no definition.

Andy White
thanks. what does the quote shown in my question "A virtual function ...." mean? Is this same as your suggestion?
Nivhus
A virtual function makes it so that any inheriting class that implements the same function (name and parameters) will have that function called. Pure virtual means that an inheriting class must implement that function in order for it to be instantiated.http://en.wikipedia.org/wiki/Virtual_function#C.2B.2B
Kyle
@Nivhus: Read my comment below my answer.
Prasoon Saurav
This is the same misunderstanding that Arun had, and my same comment applies. "You can implement a pure virtual function (which btw means derived classes must override it, not that there is no implementation). What you cannot do is place the body in the declaration."
Ben Voigt
Well, I guess I was wrong... C++ - what a great language!
Andy White
A: 

You can have a "pure" virtual function, i.e. base class has no implementation of the function

virtual void switchon() = 0;

and optionally provide an implementation which derived classes must override.

void base_class::switchon() {}

OR

you can have a "non-pure" virtual function and provide a default or empty implementation

virtual void switchon() {}
ArunSaha
Thanks. I want to know if the quote my colleague showed me is correct or not in this context
Nivhus
You can implement a pure virtual function (which btw means derived classes must override it, not that there is no implementation). What you cannot do is place the body in the declaration.
Ben Voigt
@Ben Voigt for commenting and other users who down voted: Thanks a lot. I did not knew this, learned a new thing today.
ArunSaha
Your syntax is still a little funny -- there should be no `=` in the implementation, whether pure virtual or not.
Ben Voigt
+6  A: 

A pure virtual function may have a definition(out of class definition). That is completely optional. But what you are trying to do is plain wrong because

C++03 [Section 10.4/2] says

[Note: a function declaration cannot provide both a pure-specifier and a definition —end note] [Example:

struct C {

virtual void f() = 0 { }; //ill-formed

}

However you are free to write

struct device{
   virtual void switchon()=0;
};

void device::switchon() { } //definition {optional}
int main() 
{ 

} 
Prasoon Saurav
Yes, I know this. Thanks. But I want to know if the quote my colleague showed me is correct or not in this context. Also if it is not, what does that piece of statement really mean?
Nivhus
What does it mean if something is in a [Note][Note] tag in the Standard? Generally in my opinion Notes are something that are optional to read
Nivhus
@Nivhus : It means `A virtual function can be defined in the class, can be declared pure or can be declared pure as well as can be defined(out of class definition) {optionally}`.
Prasoon Saurav
@Nivhus : Something between [Note]...[Note] must be read .:)
Prasoon Saurav
Oh. That was confusing to me. I read that statement as if it was about inline definition. Don't you guys also find it confusing or is it that I am new to reading/interpreting the Standard?
Nivhus
@Nivhus : Yes I also find the text confusing `sometimes`. Instead of reading directly from the Standard why don't you read from a good book?
Prasoon Saurav
My current challenge is to understand this concept and counterargument my colleague's interpretation of that quote. I think the discussion in this thread helps. Probably my colleague is also new immigrant in to this world of Standards (which looks interesting though)
Nivhus