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

답변됨 Can a ref class be inherited, if can, how?

  • 2012년 8월 15일 수요일 오전 10:53
     
     

    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?

모든 응답

  • 2012년 8월 15일 수요일 오전 11:42
     
     
    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

  • 2012년 8월 15일 수요일 오후 12:50
     
      코드 있음

    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
  • 2012년 8월 15일 수요일 오후 2:57
     
     답변됨
    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

    • 답변으로 표시됨 imzsf 2012년 8월 16일 목요일 오전 1:19
    • 답변으로 표시 취소됨 imzsf 2012년 8월 16일 목요일 오전 2:09
    • 답변으로 표시됨 imzsf 2012년 8월 16일 목요일 오전 7:17
    •  
  • 2012년 8월 15일 수요일 오후 9:17
    중재자
     
     답변됨 코드 있음

    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.


  • 2012년 8월 16일 목요일 오전 2:01
     
      코드 있음

    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?

  • 2012년 8월 16일 목요일 오전 6:49
    중재자
     
     답변됨

    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

    • 답변으로 표시됨 imzsf 2012년 8월 16일 목요일 오전 7:17
    •  
  • 2012년 8월 25일 토요일 오전 10:27
     
     

    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