locked
public ref class MyClass sealed : public BaseClass

    Question

  • I am writing a WinRT component in C++ which I want to use in a C# project.

    The C++ class that I want to use from C# is derived from a base class.

    To be able to use the class from C#, I must mark it as public ref class sealed.

    But to be able to derive from the base class, the base class must be public ref class, too.

    But then the compiler gives the error message that a public ref class must be sealed (which is not possible in my case because I want to derive from it) or it must be derived from a non-sealed class (what should that be?).

    I found some page on the internet about this issue but I cannot understand this issue.

    Is this a bug of Windows 8, will it be fixed in Windows 9?

    Or if not: Why is it so? And what is the solution for my problem, then?

    Saturday, November 10, 2012 4:29 PM

Answers

  • There are many approaches to solve this problem, you can return an IScenarioRenderer interface or IScenario1Renderer and IScenario2Renderer interfaces.

    public interface class IScenarioRenderer
    {
    	void Render( );
    };
    
    private ref class DirectXBase abstract
    {
    
    };
    
    private ref class Scenario1Renderer sealed : DirectXBase, IScenarioRenderer
    {
    public :
    
    	virtual void Render( ) { /* ... */ };
    };
    
    private ref class Scenario2Renderer sealed : DirectXBase, IScenarioRenderer
    {
    public :
    
    	virtual void Render( ) { /* ... */ };
    };
    
    public ref class ScenarioRendererFactory sealed
    {
    public :
    
    	static IScenarioRenderer^ CreateScenarioRenderer( ) { /* ... */ }
    };

    • Marked as answer by eikuh Wednesday, November 21, 2012 10:45 AM
    Wednesday, November 21, 2012 12:51 AM

All replies

  • Hi,

    You can not use unsealed class in Windows Runtime component, this is by design not bugs.

    For more information, please check
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh441569.aspx
     
    Best regards,
    Jesse


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

    Monday, November 12, 2012 5:49 AM
  • Hi Jesse,

    Thank you for your reply! Your link leads to a page which I already know. There, it is not explained why there was such a design decision.

    So my questions are still open:

    • Why was it designed this way? (I only see disadvantages caused by that)
    • What is the solution for my problem?
    Monday, November 12, 2012 5:29 PM
  • Frankly speaking, I was not clear why it designed this way.

    But, in your situation, you can use all C# codes for your class. As long as you do not set your class as pubic, you can inherit it in the component. But there is no way to inherit a C++ private class in component, it is the same as we can not using native C++ directly in .NET which we need p/invoke.

    The Windows Runtime component is only a wrapper, so that different language can use each other directly. But it price is that we can only use public and unsealed class with the Windows Runtime type in components.

    Best regards,
    Jesse


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

    • Proposed as answer by Dave SmitsMVP Tuesday, November 13, 2012 3:03 PM
    • Marked as answer by Jesse Jiang Monday, November 19, 2012 4:45 AM
    • Unmarked as answer by eikuh Tuesday, November 20, 2012 9:05 AM
    Tuesday, November 13, 2012 7:19 AM
  • Hi,

    WinRT is COM.

    One of the best answers is given by Nishant Sivakumar

    "In general WinRT does not encourage old fashioned inheritance chains. It's all about interfaces and implementations (like with COM)."

    • Proposed as answer by Jesse Jiang Thursday, November 15, 2012 2:48 AM
    • Marked as answer by Jesse Jiang Monday, November 19, 2012 4:45 AM
    • Unmarked as answer by eikuh Tuesday, November 20, 2012 9:05 AM
    Tuesday, November 13, 2012 11:37 AM
  • Thank you for your replies! But unfortunately, they do not solve my problem.

    I will give further details about my problem:

    My program mainly uses C#. But to be able to use DirectX, there has to be a DirectX Wrapper written in C++.

    The DirectX Wrapper consists of three classes: DirectXBase, Scenario1Renderer (inherits DirectXBase) and Scenario2Renderer (inherits DirectXBase).

    In C#, I want to use Scenario1Renderer and Scenario2Renderer, so they must be "public ref class sealed". But they have to inherit from DirectXBase (not only the interface but the implementation!). So DirectXBase would have to be "public ref class", too. But if it is public then it would have to be sealed, too. But this is not possible, of course.

    So, how to solve this problem?


    • Edited by eikuh Tuesday, November 20, 2012 9:10 AM
    Tuesday, November 20, 2012 9:09 AM
  • There are many approaches to solve this problem, you can return an IScenarioRenderer interface or IScenario1Renderer and IScenario2Renderer interfaces.

    public interface class IScenarioRenderer
    {
    	void Render( );
    };
    
    private ref class DirectXBase abstract
    {
    
    };
    
    private ref class Scenario1Renderer sealed : DirectXBase, IScenarioRenderer
    {
    public :
    
    	virtual void Render( ) { /* ... */ };
    };
    
    private ref class Scenario2Renderer sealed : DirectXBase, IScenarioRenderer
    {
    public :
    
    	virtual void Render( ) { /* ... */ };
    };
    
    public ref class ScenarioRendererFactory sealed
    {
    public :
    
    	static IScenarioRenderer^ CreateScenarioRenderer( ) { /* ... */ }
    };

    • Marked as answer by eikuh Wednesday, November 21, 2012 10:45 AM
    Wednesday, November 21, 2012 12:51 AM
  • Yes.

    Hi eikuh, I got the same question as your.

    So, I had to implement various interfaces.

    And As t-n-x says "

    Hi,

    WinRT is COM.

    One of the best answers is given by Nishant Sivakumar

    "In general WinRT does not encourage old fashioned inheritance chains. It's all about interfaces and implementations (like with COM).""

    I think that because WinRT needs to be called by another languages ,like C#, javascript. So It was design like COM.

    Wednesday, November 21, 2012 4:16 AM
  • Aaah! Now I understand. This is doable!

    I was missing the idea of the Factory. (although DirectX has a lot of them. And I was always wondering why...)

    Knowledge increased! Thank you!

    Wednesday, November 21, 2012 10:50 AM