What's the equivalent to C# static classes?
-
Is it possible to define static classes as in C#? If not, is the following sufficient:
public ref class MyStaticClass
{
public:/* a bunch of static methods */
private:
MyStaticClass() {};
~MyStaticClass() {};
MyStaticClass( const MyStaticClass% ) {};
};If I enable code analysis, I get warnings on the copy constructor and the destructor:
warning: CA1811 : Microsoft.Performance : 'MyStaticClass.MyStaticClass(MyStaticClass)' appears to have no upstream public or protected callers.
warning: CA1801 : Microsoft.Usage : Parameter '' of MyStaticClass.MyStaticClass(MyStaticClass) is never used. Remove the parameter or use it in the method body.
warning: CA1822 : Microsoft.Performance : The 'this' parameter (or 'Me' in VB) of MyStaticClass.~MyStaticClass():Void is never used. Mark the member as static (or Shared in VB) or use 'this'/'Me' in the method body or at least one property accessor, if appropriate. -
Is it possible to define static classes as in C#? If not, is the following sufficient:
public ref class MyStaticClass
{
public:/* a bunch of static methods */
private:
MyStaticClass() {};
~MyStaticClass() {};
MyStaticClass( const MyStaticClass% ) {};
};If I enable code analysis, I get warnings on the copy constructor and the destructor:
warning: CA1811 : Microsoft.Performance : 'MyStaticClass.MyStaticClass(MyStaticClass)' appears to have no upstream public or protected callers.
warning: CA1801 : Microsoft.Usage : Parameter '' of MyStaticClass.MyStaticClass(MyStaticClass) is never used. Remove the parameter or use it in the method body.
warning: CA1822 : Microsoft.Performance : The 'this' parameter (or 'Me' in VB) of MyStaticClass.~MyStaticClass():Void is never used. Mark the member as static (or Shared in VB) or use 'this'/'Me' in the method body or at least one property accessor, if appropriate.You can't declare the class static but yes, static methods and member variables will give the same behavior. Mark Mark Salsbery Microsoft MVP - Visual C++
This episode brought to you by the letter N
-
You can't declare the class static but yes, static methods and member variables will give the same behavior. Mark Mark Salsbery Microsoft MVP - Visual C++
This episode brought to you by the letter N
-
I also want to ensure that
MyStaticClass
cannot be instantiated. Do you think I should make the classsealed
in addition to what I've listed above? And do I need to worry aboutoperator=()
?sealed is good to ensure no derived classes. Off the top of my head, I'd say just having a private default constructor is sufficient. Without being able to instantiate any objects of the class, the destructor, assignment operator, and copy constructor are irrelevant. If, after some more caffeine, I change my mind about that, I'll let you know :) Mark
This episode brought to you by the letter N
-
sealed is good to ensure no derived classes. Off the top of my head, I'd say just having a private default constructor is sufficient. Without being able to instantiate any objects of the class, the destructor, assignment operator, and copy constructor are irrelevant. If, after some more caffeine, I change my mind about that, I'll let you know :) Mark
This episode brought to you by the letter N
-
OK. Thanks for the input. This is what I'm going with:
public ref class MyStaticClass sealed
{
public:/* a bunch of static properties and methods */
private:
MyStaticClass() {};
};That looks like a static class to me :) Cheers, Mark
This episode brought to you by the letter N