Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. we all <i><b>love</b></i> an inheritance question!

we all <i><b>love</b></i> an inheritance question!

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++sharepointagentic-aioop
7 Posts 4 Posters 0 Views 1 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.
  • N Offline
    N Offline
    Nick Blumhardt
    wrote on last edited by
    #1

    here goes: ---snip! // abc class Base { public: // pure virtual function virtual bool Load (UINT nID) = 0; protected: Base (UINT nID) { Load (nID); } }; class Concrete { public: Concrete (UINT nID) : Base (nID) {} bool Load (UINT nID) { // do the operation } }; Concrete c (123); // here's the problem ---/snip this looks okay to me, but the linker complains that it cannot find the definition of Base's Load() member. My reasoning is that it shouldn't need it- it's never used except when its overriden... but? does this have something to do with using a pure virtual function in a ctor? thats my guess, but it would be good if someone could point me towards the standard. i'm using VC++ 6 sp 4 thanks, Nick

    G B G 3 Replies Last reply
    0
    • N Nick Blumhardt

      here goes: ---snip! // abc class Base { public: // pure virtual function virtual bool Load (UINT nID) = 0; protected: Base (UINT nID) { Load (nID); } }; class Concrete { public: Concrete (UINT nID) : Base (nID) {} bool Load (UINT nID) { // do the operation } }; Concrete c (123); // here's the problem ---/snip this looks okay to me, but the linker complains that it cannot find the definition of Base's Load() member. My reasoning is that it shouldn't need it- it's never used except when its overriden... but? does this have something to do with using a pure virtual function in a ctor? thats my guess, but it would be good if someone could point me towards the standard. i'm using VC++ 6 sp 4 thanks, Nick

      G Offline
      G Offline
      Gerald Schwab
      wrote on last edited by
      #2

      Well this isn't much help, but I just tried your code using Borland's 5.5 compiler and it works fine. I get the same error as you when using VC++ 6 sp5.

      N 1 Reply Last reply
      0
      • G Gerald Schwab

        Well this isn't much help, but I just tried your code using Borland's 5.5 compiler and it works fine. I get the same error as you when using VC++ 6 sp5.

        N Offline
        N Offline
        Nick Blumhardt
        wrote on last edited by
        #3

        heh... thanks :) should have given it a run-over with gcc- i usually use that to weed out problems with vc++ cheers nb

        1 Reply Last reply
        0
        • N Nick Blumhardt

          here goes: ---snip! // abc class Base { public: // pure virtual function virtual bool Load (UINT nID) = 0; protected: Base (UINT nID) { Load (nID); } }; class Concrete { public: Concrete (UINT nID) : Base (nID) {} bool Load (UINT nID) { // do the operation } }; Concrete c (123); // here's the problem ---/snip this looks okay to me, but the linker complains that it cannot find the definition of Base's Load() member. My reasoning is that it shouldn't need it- it's never used except when its overriden... but? does this have something to do with using a pure virtual function in a ctor? thats my guess, but it would be good if someone could point me towards the standard. i'm using VC++ 6 sp 4 thanks, Nick

          B Offline
          B Offline
          Ben Burnett
          wrote on last edited by
          #4

          > does this have something to do with using a pure virtual function in a ctor? I imagine your right. I think that if you call a pure virtual member within an a class that declares it, you are telling the linker to find the member it points to (in the VTABLE). The linker chokes because you have not defined one (obviously since it's declared as a pure virtual). BTW: Why the protected ctor? Since Base has a pure virtual in it, it will never be able to be instantiated anyway. -Ben "Its funny when you stop doing things not because they’re wrong, but because you might get caught." - Unknown

          1 Reply Last reply
          0
          • N Nick Blumhardt

            here goes: ---snip! // abc class Base { public: // pure virtual function virtual bool Load (UINT nID) = 0; protected: Base (UINT nID) { Load (nID); } }; class Concrete { public: Concrete (UINT nID) : Base (nID) {} bool Load (UINT nID) { // do the operation } }; Concrete c (123); // here's the problem ---/snip this looks okay to me, but the linker complains that it cannot find the definition of Base's Load() member. My reasoning is that it shouldn't need it- it's never used except when its overriden... but? does this have something to do with using a pure virtual function in a ctor? thats my guess, but it would be good if someone could point me towards the standard. i'm using VC++ 6 sp 4 thanks, Nick

            G Offline
            G Offline
            Gert Boddaert
            wrote on last edited by
            #5

            Do NOT call a (pure) virtual function in a constructor or destructor. This can result in undefined behavior. Think about it, or grab a copy of 'Stroustrup'. -------------------------------------------------- If my messages appear curt, I apologize. I try to be brief to save your time as well as mine. --------------------------------------------------

            N 1 Reply Last reply
            0
            • G Gert Boddaert

              Do NOT call a (pure) virtual function in a constructor or destructor. This can result in undefined behavior. Think about it, or grab a copy of 'Stroustrup'. -------------------------------------------------- If my messages appear curt, I apologize. I try to be brief to save your time as well as mine. --------------------------------------------------

              N Offline
              N Offline
              Nick Blumhardt
              wrote on last edited by
              #6

              Thanks, Now that I think about it... Base ctor called first, pure virtual function call would open the possibility for using unconstructed subclass data... mmm makes me worried that Borland C++ DID compile it! Thanks again to all of you :rose: nick

              G 1 Reply Last reply
              0
              • N Nick Blumhardt

                Thanks, Now that I think about it... Base ctor called first, pure virtual function call would open the possibility for using unconstructed subclass data... mmm makes me worried that Borland C++ DID compile it! Thanks again to all of you :rose: nick

                G Offline
                G Offline
                Gerald Schwab
                wrote on last edited by
                #7

                I found this in the MSDN online help :

                Another restriction is that if the constructor for an abstract class calls a pure virtual function, either directly or indirectly, the result is undefined.
                http://msdn.microsoft.com/library/devprods/vs6/visualc/vclang/_pluslang_restrictions_on_using_abstract_classes.htm
                Borland compiled without errors, but didn't run correctly. I added printf function calls to the constructors and destructors of both classes. When I ran the program, the printf functions didn't output anything. When I removed the call for "Load()" in the base class, all worked as it should. I guess you could say that MSVC++ works better since it doesn't allow to shoot yourself in the foot.:)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups