locked
Why it shows the error in VS2012 RC: "an unsealed ref class cannot have a public visible constructor"

Answers

  • If you really need to have that class unsealed, move his constructor to private or private protected.
    Tuesday, July 10, 2012 9:03 AM
  • The Consumer Preview to Release Preview Migration Guide points this out under the section "Public ref classes need to be marked as either ‘sealed’ or have a non-public constructor".

    David Lamb

    • Marked as answer by tornadory Wednesday, July 11, 2012 1:49 AM
    Tuesday, July 10, 2012 11:45 PM
    Moderator

All replies

  • If you really need to have that class unsealed, move his constructor to private or private protected.
    Tuesday, July 10, 2012 9:03 AM
  • The Consumer Preview to Release Preview Migration Guide points this out under the section "Public ref classes need to be marked as either ‘sealed’ or have a non-public constructor".

    David Lamb

    • Marked as answer by tornadory Wednesday, July 11, 2012 1:49 AM
    Tuesday, July 10, 2012 11:45 PM
    Moderator
  • hi,

    Thank you and Petr. 

    But the class (Button) need to be inherited, if it was marked as sealed or move its constructor to private or private protected, how could I create another class which inherit it?

    I can not understand why those ref classes could not be inherited? If I missed something, please help to explain. What should I do if I need inherit these Classes?

    Wednesday, July 11, 2012 1:21 AM
  • Ah, I see the code sample in the Chinese language document has not been updated. The English version has the proper code samples for Release Preview:

    http://technet.microsoft.com/en-us/library/hh699894(v=vs.110)


    David Lamb


    Wednesday, July 11, 2012 1:40 AM
    Moderator
  • Sorry, I found the option to solve the problem in the document.

    Thank you.

    (a) If your class was not meant to be a base class, do mark the class as “sealed” (see added keyword in green):
    public ref class Foo sealed
    {
    public:
        Foo();
    };
    (b) If you want to allow other C++ ref classes in the same compilation unit to inherit from the class and if the derived classes don’t need to be public, simply remove public from the base class:
    public ref class Foo 
    {
    };
    ref class Bar: Foo
    {
    };
    (c) If you still want to allow other C++ ref classes in the same compilation unit to inherit from the class  and one or more of the derived classes has to be public, mark the constructor of the base class as internal. This will allow C++ in-module derivation, and output both the base class and derived class to the .winmd. However, since the constructor of the base is private, the base class will be neither activatable, nor composable from outside the module (i.e. the base class becomes a NARC – Non-Activatable-Runtime-Class):
    public ref class Foo 
    {
    // in case no constructor is provided, the implicit
    // constructor will be internal
    internal:
        Foo();
    };
    public ref class Bar: Foo
    {
    };
    

    Wednesday, July 11, 2012 1:52 AM