Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. What's the equivalent to C# static classes?

What's the equivalent to C# static classes?

Scheduled Pinned Locked Moved Managed C++/CLI
csharpperformancequestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mid 5741
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • M mid 5741

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        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

        M Offline
        M Offline
        mid 5741
        wrote on last edited by
        #3

        I also want to ensure that MyStaticClass cannot be instantiated. Do you think I should make the class sealed in addition to what I've listed above? And do I need to worry about operator=()?

        M 1 Reply Last reply
        0
        • M mid 5741

          I also want to ensure that MyStaticClass cannot be instantiated. Do you think I should make the class sealed in addition to what I've listed above? And do I need to worry about operator=()?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • M Mark Salsbery

            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

            M Offline
            M Offline
            mid 5741
            wrote on last edited by
            #5

            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() {};
            };

            M 1 Reply Last reply
            0
            • M mid 5741

              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() {};
              };

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              That looks like a static class to me :) Cheers, Mark

              This episode brought to you by the letter N

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups