locked
Static classes in C++.NET RRS feed

  • Question

  • How do you define static classes in C++.NET?
    Like how do you write the following C# code in C++.NET:

    public static class Class1  
    {  
      public const int Var1 = 0;  
    }  
     
    public class Class2  
    {  
      public static class Class3  
      {  
      }  
    }  
     


    Simply adding the "static" keyword has odd effects. In an outer class it generates a syntax error. In an inner class it compiles, but doesn't create a static class (seems to still be an instancible class).
    Friday, August 15, 2008 5:29 PM

Answers

  • The .NET framework has no notion of a static class.  You can see this in the properties for the Type class.  There is no IsStatic property like there is for, say, FieldInfo.  The C# keyword is just for decoration to catch mistakes.  The best way to simulate static classes in C++/CLI is by declaring them abstract and sealed:

    #include "stdafx.h"

    public ref class MyStaticClass abstract sealed {
    public:
      static int member;
    };

    public ref class TryInherit : MyStaticClass {};  // C3246, can't inherit

    int main(array<System::String ^> ^args)
    {
      MyStaticClass::member = 1;                    // Okay
      MyStaticClass^ obj = gcnew MyStaticClass;     // C3622, can't instantiate
      return 0;
    }


    Hans Passant.
    • Marked as answer by Magos Saturday, August 16, 2008 8:34 AM
    Saturday, August 16, 2008 12:55 AM

All replies

  • You don't use the static keyword on classes in C++.  You have static members and methods that you call.  Take a look at http://www.functionx.com/managedcpp/keywords/static.htm.

    Michael Fischer
    Friday, August 15, 2008 7:15 PM
  • Well, assuming I have this in a C++.NET class library:

    public ref class CppStaticClass  
    {  
      public:  
        literal int X = 0; //Equal to C# "const int"
        static initonly int Y = 0; //Equal to C# "static readonly int"
    }; 

    When using this in a C# project I can still create an instance of the class:

    CppStaticClass dummy = new CppStaticClass(); 

    How do I treat the class as a static non-instancable class? I tried removing the "ref" keyword but then it can't contain managed stuff.
    Friday, August 15, 2008 8:30 PM
  • Include a private constructor in the class.
    Convert between VB, C#, C++, and Java (http://www.tangiblesoftwaresolutions.com)
    Saturday, August 16, 2008 12:34 AM
  • The .NET framework has no notion of a static class.  You can see this in the properties for the Type class.  There is no IsStatic property like there is for, say, FieldInfo.  The C# keyword is just for decoration to catch mistakes.  The best way to simulate static classes in C++/CLI is by declaring them abstract and sealed:

    #include "stdafx.h"

    public ref class MyStaticClass abstract sealed {
    public:
      static int member;
    };

    public ref class TryInherit : MyStaticClass {};  // C3246, can't inherit

    int main(array<System::String ^> ^args)
    {
      MyStaticClass::member = 1;                    // Okay
      MyStaticClass^ obj = gcnew MyStaticClass;     // C3622, can't instantiate
      return 0;
    }


    Hans Passant.
    • Marked as answer by Magos Saturday, August 16, 2008 8:34 AM
    Saturday, August 16, 2008 12:55 AM
  • The "abstract sealed" seems to be the C++ equivalent to C# "static". The intellisence gives the same info about the classes. I didn't even think of using abstract sealed together, it's invalid in C# :).
    Thanks!

    PS: You're unable to use the "literal" keyword in an abstratc sealed class though. I read somewhere that it's the C++ equivalent to C# const.

    EDIT: Hm, this is actually a bug, as explained at https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282773
    • Edited by Magos Saturday, August 16, 2008 8:45 AM Note about the problem
    Saturday, August 16, 2008 8:36 AM