Re: downcasting template class with paramaterised parent
<jason.saunders@gmail.com> wrote:
Is it not possible, therefore, to do what I want to do
using templates?
I want to be able to both inherit from some MFC classes
but provide a
common base class.
Since I can not modify the MFC classes to be derived frrom
my common
class I figured the only way to do it was using templates.
You can encapsulate common functionality in base template
class as you did in original post. However, in order to cast
to this class, you need a type to cast to. In your case you
have following types:
CEdit, CButton - base types
MyControl<CEdit>, MyControl<CButton> - common base
MyEdit, MyButton - derived types
That's all. You can cast CWnd* pointer to either of them,
but not to MyControl<???>.
Alternatively, you can create common base type for your
control and inherit from it along with MFC class. Although
MFC has its own idiosyncrasies regarding multiple
inheritance, it's doable and can work. For more information
see here:
"TN016: Using C++ Multiple Inheritance with MFC"
http://msdn2.microsoft.com/en-us/library/62x3wzxy.aspx
HTH
Alex