Pure virtual function with implementation
-
wrote on 6 Jan 2020, 14:47 last edited by
Hello All,
I have a basic doubt,
-> why class which has pure virtual function (with function definition of it declared outside class) still cannot instantiate an object ??Is it not the pure virtual function definition's address which is declared outside of class be mapped inside V-Table ?? or am i missing understanding basic concept on this topic ?
Thank you!
-
Hi,
Whether pure virtual or pure virtual with implementation does not matter. At the moment a method is pure virtual the class becomes abstract and must be subclassed. The pure virtual method with implementation allows the base class to provide a more or less sensible implementation but forces the inherited class to either implement something or just call the base class implementation if it doesn't want to do anything special. It's not an often used feature of the language.
-
@SGaist said in Pure virtual function with implementation:
The pure virtual method with implementation
Either a function is pure virtual (= 0) or virtual (=has an implementation).
-
Lifetime Qt Championwrote on 6 Jan 2020, 18:36 last edited by SGaist 1 Jun 2020, 18:38
@Christian-Ehrlicher There really is pure virtual with definition. See the CPP Reference about abstract classes especially the part on pure virtual, just under the seconde code sample.
-
@Christian-Ehrlicher There really is pure virtual with definition. See the CPP Reference about abstract classes especially the part on pure virtual, just under the seconde code sample.
wrote on 6 Jan 2020, 20:02 last edited by JonB 1 Jun 2020, 20:03@SGaist
Interesting! Having read through your link, I Googled around and found https://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/ summarise with:This capability isn’t used very commonly.
:)
I would refer the interested reader to: https://stackoverflow.com/questions/2609299/use-cases-of-pure-virtual-functions-with-body
-
@Christian-Ehrlicher There really is pure virtual with definition. See the CPP Reference about abstract classes especially the part on pure virtual, just under the seconde code sample.
@SGaist said in Pure virtual function with implementation:
There really is pure virtual with definition.
There indeed is such a thing, but nobody in his right mind really uses it. Never have I needed it in my ~15 years of programming, nor would I encourage it. It breaks the semantics of a virtual being "pure". In my mind you either have a default implementation (which means simply
virtual
) or you expect the user to provide one (virtual = 0
). -
@SGaist said in Pure virtual function with implementation:
There really is pure virtual with definition.
There indeed is such a thing, but nobody in his right mind really uses it. Never have I needed it in my ~15 years of programming, nor would I encourage it. It breaks the semantics of a virtual being "pure". In my mind you either have a default implementation (which means simply
virtual
) or you expect the user to provide one (virtual = 0
).wrote on 6 Jan 2020, 20:44 last edited by@kshegunov said in Pure virtual function with implementation:
There indeed is such a thing, but nobody in his right mind really uses it.
You are right, but nonetheless in the stackoverflow reference I gave 7 people bothered to respond with a use case :)
-
@kshegunov said in Pure virtual function with implementation:
There indeed is such a thing, but nobody in his right mind really uses it.
You are right, but nonetheless in the stackoverflow reference I gave 7 people bothered to respond with a use case :)
Moderatorswrote on 6 Jan 2020, 20:51 last edited by kshegunov 1 Jun 2020, 20:51@JonB said in Pure virtual function with implementation:
You are right, but nonetheless in the stackoverflow reference I gave 7 people bothered to respond with a use case :)
Most, if not all, of whom agree that neither is the feature much useful, nor are arguments "for" its use convincing. And btw, the code example where a class is derived from the base just to provide the default implementation is as bogus as hell. I'm going to say it again - nobody in his right mind does that ...
-
@SGaist said in Pure virtual function with implementation:
There really is pure virtual with definition.
There indeed is such a thing, but nobody in his right mind really uses it. Never have I needed it in my ~15 years of programming, nor would I encourage it. It breaks the semantics of a virtual being "pure". In my mind you either have a default implementation (which means simply
virtual
) or you expect the user to provide one (virtual = 0
).@kshegunov said in Pure virtual function with implementation:
@SGaist said in Pure virtual function with implementation:
There really is pure virtual with definition.
There indeed is such a thing, but nobody in his right mind really uses it. Never have I needed it in my ~15 years of programming, nor would I encourage it. It breaks the semantics of a virtual being "pure". In my mind you either have a default implementation (which means simply
virtual
) or you expect the user to provide one (virtual = 0
).I never said I was encouraging its use ;-)
-
@kshegunov said in Pure virtual function with implementation:
@SGaist said in Pure virtual function with implementation:
There really is pure virtual with definition.
There indeed is such a thing, but nobody in his right mind really uses it. Never have I needed it in my ~15 years of programming, nor would I encourage it. It breaks the semantics of a virtual being "pure". In my mind you either have a default implementation (which means simply
virtual
) or you expect the user to provide one (virtual = 0
).I never said I was encouraging its use ;-)
Moderatorswrote on 6 Jan 2020, 22:50 last edited by kshegunov 1 Jun 2020, 22:50@SGaist said in Pure virtual function with implementation:
I never said I was encouraging its use ;-)
I never said, you said such a thing. ;)
I was just in a verbose mood ... ;P -
wrote on 6 Jan 2020, 23:51 last edited by fcarney 1 Jun 2020, 23:56
I think I used it in a windowing type library to define a BaseWindow that didn't actually draw anything. It was there to provide a pointer to all classes Window related. I cannot remember if I could actually instantiate it or not. This was 20 years ago, so details for fuzzy and this was a windowing library for DOS.Edit:
Maybe I misread the question, but I don't think I defined any functions for pure virtual class.Edit2:
Okay, I read the SO question and realized that is just really strange thing to do. So no, I don't think I have done this.
1/11