locked
Can a ref class be inherited, if can, how?

    Question

  • I declare a interface in my WinRT project, and a ref class A inherit from the interface, so A is sealed.

    Now, I want to declare a ref class B that inherit from A, any ideas?

    Wednesday, August 15, 2012 10:53 AM

Answers

  • Yeah, you may have to make it non-public, or introduce a dummy base class that's private just so you can derive A from that and then make it public.  In general WinRT does not encourage old fashioned inheritance chains. It's all about interfaces and implementations (like with COM).

    http://blog.voidnish.com

    • Marked as answer by imzsf Thursday, August 16, 2012 1:19 AM
    • Unmarked as answer by imzsf Thursday, August 16, 2012 2:09 AM
    • Marked as answer by imzsf Thursday, August 16, 2012 7:17 AM
    Wednesday, August 15, 2012 2:57 PM
  • Any unsealed public ref class must have an unsealed Windows Runtime base class in it's inheritance hierarchy.

    e.g.

    	public ref class A : public Windows::UI::Xaml::DependencyObject, ICom
    	{
    	  // ...
    	};

    The RTM compiler now enforces that all unsealed public ref classes have a base class which is also unsealed.


    Wednesday, August 15, 2012 9:17 PM
    Moderator
  • Hi,

    Platform::Objectis sealed; it is not a universal base class.
    After you have defined an unsealed classMyBasethat inherits fromDependencyObject, other public or private ref classes in your component or app may inherit fromMyBase. I

    Please take a look Inheritance section in this document
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh699870.aspx

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by imzsf Thursday, August 16, 2012 7:17 AM
    Thursday, August 16, 2012 6:49 AM

All replies

  • A sealed class cannot be further inherited. You can remove the sealed from class-A and now class-B can derive from A, but publicly exposed RT types are expected to be sealed. So while you may be able to access A across WinRT, it's not an officially supported scenario and has a few issues associated with it (that's been reported in these forums).

    http://blog.voidnish.com

    Wednesday, August 15, 2012 11:42 AM
  • hi,

    thank you for your reply.

    but when I remove sealed from class-A, it build with an error C4585 : A WinRT 'public ref class' must either be sealed or derive from an existing unsealed class.

    for more information, my code like this:

    public interface class ICom
    {
       ...
    }
    
    public ref class A : public ICom
    {
       ...
    }
    
    public ref class B sealed : public A
    {
       ...
    }

    maybe it can build success in Win8 build8400 + VS2012 RC, but I build with the error in Win8 build9200 + VS2012 RTM
    Wednesday, August 15, 2012 12:50 PM
  • Yeah, you may have to make it non-public, or introduce a dummy base class that's private just so you can derive A from that and then make it public.  In general WinRT does not encourage old fashioned inheritance chains. It's all about interfaces and implementations (like with COM).

    http://blog.voidnish.com

    • Marked as answer by imzsf Thursday, August 16, 2012 1:19 AM
    • Unmarked as answer by imzsf Thursday, August 16, 2012 2:09 AM
    • Marked as answer by imzsf Thursday, August 16, 2012 7:17 AM
    Wednesday, August 15, 2012 2:57 PM
  • Any unsealed public ref class must have an unsealed Windows Runtime base class in it's inheritance hierarchy.

    e.g.

    	public ref class A : public Windows::UI::Xaml::DependencyObject, ICom
    	{
    	  // ...
    	};

    The RTM compiler now enforces that all unsealed public ref classes have a base class which is also unsealed.


    Wednesday, August 15, 2012 9:17 PM
    Moderator
  • Yeah, your e.g. is right, thank you.

    but I try to derive from Platform::Object, it's also an unsealed WinRT class

    e.g.

    public ref class A : public Platform::Object, ICom
    {
      //...
    }

    I also get an error C4585 : A WinRT 'public ref class' must either be sealed or derive from an existing unsealed class.

    why?

    Thursday, August 16, 2012 2:01 AM
  • Hi,

    Platform::Objectis sealed; it is not a universal base class.
    After you have defined an unsealed classMyBasethat inherits fromDependencyObject, other public or private ref classes in your component or app may inherit fromMyBase. I

    Please take a look Inheritance section in this document
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh699870.aspx

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by imzsf Thursday, August 16, 2012 7:17 AM
    Thursday, August 16, 2012 6:49 AM
  • Hi,

    Platform::Objectis sealed; it is not a universal base class.
    After you have defined an unsealed classMyBasethat inherits fromDependencyObject, other public or private ref classes in your component or app may inherit fromMyBase. I

    Please take a look Inheritance section in this document
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh699870.aspx

    In C# classes implicitly derive from object; is there a comparable C++ object that we can derive from as a best practice?   The link sample shows a class deriving from a DependencyObject but this does not fit in with the function of a Presenter base; I'm using it for now to quiet the compiler....


    MCAD.NET C# - http://www.Global-webnet.com/Blog

    Saturday, August 25, 2012 10:27 AM