4

Consider:

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

int main()
{

}

I wrote code similar to following and it gave an 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.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nivhus
  • 133
  • 7
  • 3
    Please see http://stackoverflow.com/questions/2951273/pure-specifier-on-function-definition – czchen Oct 01 '10 at 02:39
  • 1
    Hmm. Confirms my suspicion. The Wiki link points to Standard C++ 98 - 10.4/2. Here it is mentioned "[ Note: a function declaration cannot provide both a pure-specifier and a definition —end note ]" which clarifies the situation. So it is not the quote my colleague showed me – Nivhus Oct 01 '10 at 02:44
  • @Nivhus : Yes `10.4/2` is the correct/relevant quote. – Prasoon Saurav Oct 01 '10 at 02:48
  • What the quote from the Standard means is that it's an error to fail to define a virtual function which is not pure virtual. Different but related requirement. – aschepler Oct 01 '10 at 16:27

2 Answers2

5

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()
{

}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 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 Oct 01 '10 at 02:48
  • 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 Oct 01 '10 at 02:51
  • 2
    @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 Oct 01 '10 at 02:51
  • 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 Oct 01 '10 at 02:55
  • @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 Oct 01 '10 at 02:56
  • 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 Oct 01 '10 at 02:58
-1

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() {}
Arun
  • 19,750
  • 10
  • 51
  • 60
  • Thanks. I want to know if the quote my colleague showed me is correct or not in this context – Nivhus Oct 01 '10 at 02:47
  • 1
    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 Oct 01 '10 at 03:45
  • @Ben Voigt for commenting and other users who down voted: Thanks a lot. I did not knew this, learned a new thing today. – Arun Oct 01 '10 at 16:17
  • Your syntax is still a little funny -- there should be no `=` in the implementation, whether pure virtual or not. – Ben Voigt Oct 01 '10 at 17:01