In short what I want to do is have an Abstract Base class call a method implemented in a subclass. I am sure I have done this at some point in the past, but I can't remember how. Anyway some sample code.
Abstract Base Class:Code:#pragma once class CAbstractBase { public: CAbstractBase(void); ~CAbstractBase(void); protected: CString MyClassName; virtual void SetClassName()=0; };SubClass:Code:#include "StdAfx.h" #include "AbstractBase.h" CAbstractBase::CAbstractBase(void) { SetClassName(); } CAbstractBase::~CAbstractBase(void) { } // If this method is commented out, I get a linker error, that this // symbol CAbstractBase::SetClassName(void) is missing void CAbstractBase::SetClassName() { MyClassName = "CAbstractBase"; }Code:#pragma once #include "abstractbase.h" class CSubClass : public CAbstractBase { public: CSubClass(void); ~CSubClass(void); virtual void SetClassName(); };Program code:Code:#include "StdAfx.h" #include "SubClass.h" CSubClass::CSubClass(void) { } CSubClass::~CSubClass(void) { } void CSubClass::SetClassName() { MyClassName = "CSubClass"; }I put a break point on the T = 3 assignment.Code:CSubClass AClass; int T = 3;
Anyway run as is, MyClassName == "CAbstractBase", not "CSubClass" which is the desired result. If I comment out the SetClassName() from the abstract base class, I get a linker error. I am not sure why since the base class will never be instanced because it is abstract. If I define the function, I get the wrong class name ( I kind of expected that to happen).
Is there a way to get the base class to call the sub-class method?


Reply With Quote
