-4

I have searched on google and here but I can not understand why a pure function in a class must be virtual. I understand maybe it is not very useful declare a "normal function" as pure but I don't think is a nonsense. I mean, the word "pure" is there just to declare an abstract class. Ok, I can not use polymorphism with that pure normal function but the main reason (declare a class as abstract) is reached anyway. Am I wrong?

andre3312
  • 147
  • 7

2 Answers2

5

There is no requirement that a pure function be virtual. If you're thinking of the term "pure virtual function", "pure" applies to "virtual" there; the function is purely virtual. It's a different use of the word "pure".

I mean, the word "pure" is there just to declare an abstract class.

It's not. The reason for declaring a pure virtual function is not to prevent instantiation of the enclosing class; it's to ensure that concrete subclasses implement the method, usually because the abstract class cannot provide a reasonable implementation.

Ok, I can not use polymorphism with that pure normal function but the main reason (declare a class as abstract) is reached anyway.

If you're looking for a way to declare a class abstract without any pure virtual functions, C++ does not have dedicated syntax for that. The suggestions I've seen are to declare and implement a pure virtual destructor (you can do that), or to make the constructor protected (but making the constructor protected won't make your class pass std::is_abstract). In any case, it wouldn't make much sense to attach the syntax for that to arbitrary member functions; something like class Foo = 0 { ... }; would make more sense.

Community
  • 1
  • 1
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • I don't understand. I tried to create a simple class with a normal pure function (for example: void fun() = 0) and the compiler raised an error. – andre3312 May 11 '16 at 16:14
  • @andre3312: "= 0" is not the syntax to declare that a function is pure; it's the syntax to declare that a virtual function is purely virtual. Trying to put it on a non-virtual function is like trying to say that a burger is "very food" instead of "very tasty food" or "very unhealthy food". – user2357112 May 11 '16 at 16:16
  • *There is no requirement that a pure function be virtual.* Not true. Only `virtual` member functions can be pure. – R Sahu May 11 '16 at 16:17
  • 1
    @RSahu: "Pure function" refers to [a function whose return value depends only on its parameters, and which produces no side effects](https://en.wikipedia.org/wiki/Pure_function). There is no requirement that such a function be virtual. You're thinking of pure virtual functions, an entirely unrelated concept. – user2357112 May 11 '16 at 16:19
  • 1
    @RSahu "Pure functions" are typically what functions in functional languages are referred to. A pure function is a function with no side effects. – erip May 11 '16 at 16:19
  • That's not the kind of "pure" the OP is talking about. They are talking about the creation of pure virtual functions using the `= 0;` specifier. – R Sahu May 11 '16 at 16:21
  • 1
    @RSahu Fair, but your qualm was with the statement that pure functions must be virtual - they don't have to be. Semantics are important. :) It's also hard to understand as a concept because "pure functions" wrt OO _don't exist_. – erip May 11 '16 at 16:22
  • @erip, in the context of C++, that's a correct assertion. – R Sahu May 11 '16 at 16:24
  • 1
    No, it's not. That's like saying C pointers are smart pointers because they're used in a good way. It's conflating two terms that have very different meanings. – erip May 11 '16 at 16:26
  • Yes, this is correct answer. Pure function is not a concept which exists in C++. – SergeyA May 11 '16 at 16:30
  • 7
    I think this would could much less confusion if people called them by their grammatical name. They're _purely virtual_ functions. – erip May 11 '16 at 16:30
  • It's really annoying that you are biased / feel attacked because of the word `pure`. Yes, pure virtual functions _do_ exist in C++, period. – Hatted Rooster May 11 '16 at 16:33
  • 1
    @GillBates I don't think you understand the argument here. – erip May 11 '16 at 16:34
  • 1
    @erip, I can see that English is your native language! :) Or you just good at english grammar. – SergeyA May 11 '16 at 16:34
  • @SergeyA Not all native speakers have a good grasp of English grammar... trust me. :P – erip May 11 '16 at 16:35
  • @erip I do, people are bitching about `pure` as opposed to `purely`, we all know what OP meant and bitching about something like that is childish at best. – Hatted Rooster May 11 '16 at 16:36
  • @GillBates No one is arguing that _pure virtual_ functions don't exist. We are arguing that _pure_ functions, in the C++ OO paradigm, do not exist _as described by OP_. `pure functions != "pure functions" != pure virtual functions` – erip May 11 '16 at 16:37
  • @erip As I said before, OP obviously meant pure virtual functions as opposed to pure functions. The context gives that away. – Hatted Rooster May 11 '16 at 16:37
  • @GillBates The question is about semantics; specifically about a misunderstanding with respect to those regarding "pure functions." To address the question, we must define "pure functions." The resolution to this question is simply that "pure functions" don't exist in C++. The confusion lies in that pure describes virtual, not function - hence my choice of calling them _purely virtual_ functions. – erip May 11 '16 at 16:40
  • @erip Yes I understand your point but from the following passage "Ok, I can not use polymorphism with that pure normal function but the main reason (declare a class as abstract) is reached anyway" You can clearly tell OP means purely virtual functions. My rant wasn't necessarily against you but rather to the people that purposely took the word `pure` out of context. There was no confusion OP was talking about `purely virtual` functions. – Hatted Rooster May 11 '16 at 16:42
  • 1
    @GillBates: If the OP really was talking about purely virtual functions, then the question in the title is nonsense. It becomes, "In C++, why must a purely virtual function be virtual?" – Benjamin Lindley May 11 '16 at 16:54
  • @BenjaminLindley Well yeah, it can be. I never said it wasn't but judging from " but the main reason (declare a class as abstract)" it's definitely not about pure functions. – Hatted Rooster May 11 '16 at 16:57
4

The only reason you'd want to have a "pure function" is to make sure subclasses that inherit from this class define an implementation of this function. If you would allow pure functions to be non-virtual and thus not being able to be overriden they are basically pointless to have.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • But I can override a function not virtual. – andre3312 May 11 '16 at 16:18
  • I think, this answer undully assumes that 'pure' functions exist. – SergeyA May 11 '16 at 16:18
  • 1
    @andre3312, you can. However, the function call won't be dispatched to the derived class implementation if the call is made through a base class pointer or object. – R Sahu May 11 '16 at 16:19
  • @SergeyA No, I'm saying that a pure function that has no `virtual` wouldn't make sense, `pure virtual` functions exist yes. Or `pure virtual` methods if you want to get specific. – Hatted Rooster May 11 '16 at 16:19
  • Again, to have 'pure' functions, language should have 'unpure' functions. Every function in C++ is a pure function, because non of them are unpure. – SergeyA May 11 '16 at 16:22
  • @SergeyA Not sure if I understand you here, a function that does not contain a body and has `= 0` after it's signature is a pure virtual function. Once that does not is not a pure virtual function. What do you mean with "every function in C++ is a pure function because non of them are unpure" ? – Hatted Rooster May 11 '16 at 16:27
  • What I am saying is that C++ doesn't have the concept of pure functions. Period. They do not exist. – SergeyA May 11 '16 at 16:28
  • @SergeyA Then how would you name a function that has `= 0` after it and is vritual ? `equals null functions` ? – Hatted Rooster May 11 '16 at 16:32
  • No, those are pure virtual function. Where *pure* relates to *virtual*, not *function*. See my comments under the question and the answer from @user2357112 – SergeyA May 11 '16 at 16:33
  • @SergeyA That's what I _literally_ just said, you're just being silly now. "Not sure if I understand you here, a function that does not contain a body and has = 0 after it's signature is a pure virtual function." to which you respond : "No, those are pure virtual function" What? – Hatted Rooster May 11 '16 at 16:35
  • No is for 'equals null functions'. Last time. While **pure virtual functions** exist, **pure functions** do not. – SergeyA May 11 '16 at 16:36
  • Yeah nvm, not worth arguing over. – Hatted Rooster May 11 '16 at 16:36
  • 2
    @andre3312: _"But I can override a function not virtual."_ No, you can't. – Lightness Races in Orbit May 11 '16 at 16:55
  • @LightnessRacesinOrbit: Are you telling I can't override a function in a derived class? – andre3312 May 11 '16 at 20:21
  • @LightnessRacesinOrbit: Maybe it is not a good practice but it is not prohibited – andre3312 May 11 '16 at 20:28
  • @andre3312: No, it's literally impossible. You're mistaking _name hiding_ for overriding. – Lightness Races in Orbit May 12 '16 at 00:18
  • @LightnessRacesinOrbit: Please have a look at this. Question number 10 of the link. https://isocpp.org/wiki/faq/strange-inheritance#redefining-nonvirtuals. – andre3312 May 12 '16 at 07:48
  • @andre3312: That's not actually overriding. The word "override" is in quotes there to indicate that, but really they should have avoided it altogether. Remember, a collaboratively edited Wiki on the internet is not authoritative; the ISO standard is. – Lightness Races in Orbit May 12 '16 at 08:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111722/discussion-between-andre3312-and-lightness-races-in-orbit). – andre3312 May 12 '16 at 09:04