Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Pure virtual function with implementation
Forum Updated to NodeBB v4.3 + New Features

Pure virtual function with implementation

Scheduled Pinned Locked Moved Unsolved C++ Gurus
11 Posts 6 Posters 2.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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).

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

          @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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ kshegunovK 2 Replies Last reply
          3
          • SGaistS SGaist

            @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.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @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

            1 Reply Last reply
            0
            • SGaistS SGaist

              @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.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @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).

              Read and abide by the Qt Code of Conduct

              JonBJ SGaistS 2 Replies Last reply
              1
              • kshegunovK kshegunov

                @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).

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @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 :)

                kshegunovK 1 Reply Last reply
                0
                • JonBJ JonB

                  @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 :)

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @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 ...

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • kshegunovK kshegunov

                    @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).

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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 ;-)

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    kshegunovK 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @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 ;-)

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #10

                      @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

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • fcarneyF Offline
                        fcarneyF Offline
                        fcarney
                        wrote on last edited by fcarney
                        #11

                        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.

                        C++ is a perfectly valid school of magic.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved