we all <i><b>love</b></i> an inheritance question!
-
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
-
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
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.
-
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.
heh... thanks :) should have given it a run-over with gcc- i usually use that to weed out problems with vc++ cheers nb
-
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
> 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
-
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
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. --------------------------------------------------
-
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. --------------------------------------------------
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
-
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
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.:)