CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004
    Posts
    1,361

    Question Abstract base class and subclasses question.

    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;
    };
    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";
    }
    SubClass:
    Code:
    #pragma once
    #include "abstractbase.h"
    
    class CSubClass :
    	public CAbstractBase
    {
    public:
    	CSubClass(void);
    	~CSubClass(void);
    
    	virtual void SetClassName();
    };
    Code:
    #include "StdAfx.h"
    #include "SubClass.h"
    
    CSubClass::CSubClass(void)
    {
    }
    
    CSubClass::~CSubClass(void)
    {
    }
    
    void CSubClass::SetClassName()
    {
    	MyClassName = "CSubClass";
    }
    Program code:
    Code:
    CSubClass AClass;
    
    		int T = 3;
    I put a break point on the T = 3 assignment.

    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?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Abstract base class and subclasses question.

    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Abstract base class and subclasses question.

    Code:
    CAbstractBase::CAbstractBase(void)
    {
    	SetClassName();
    }
    You should not call virtual method in base class constructor as the descendant is not constructed yet.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured