Results 1 to 5 of 5

Thread: Is there the static class in C++?

  1. #1
    Join Date
    Jan 2006
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Is there the static class in C++?

    When I read some documents about the design, I usually meet this word but I'm not very familiar with it in C++.
    Thanks for caring!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Is there the static class in C++?

    I don't know what you mean by "static class".
    In C++ you can have static members or functions in a class. These are not bound to any instance but instead are "class global".

    Definition:
    Qt Code:
    1. class C
    2. {
    3. public:
    4. static int a;
    5. static void f() {cout << a << "\n";}
    6. void m() {cout << a << "\n";}// normal method
    7. };
    8.  
    9. int C::a = 21;
    To copy to clipboard, switch view to plain text mode 

    Usage:
    Qt Code:
    1. C c1, c2;
    2. cout << c1.a << " " << c2.a << "\n";// normal way to access
    3. c1.a = 42;
    4. cout << C::a << "\n";// static access
    5. C::f();// static access
    6. c1.f(); c2.f();
    7. C::a = 21;
    8. c1.g(); c2.g();
    To copy to clipboard, switch view to plain text mode 


    And there's another meaning of static when refering to variables in implementation files: These variables are file-local when declared static.

  3. #3
    Join Date
    Jan 2006
    Location
    Winnipeg, MB. Canada
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there the static class in C++?

    Possibly confusing C++ with C#? A C# class (since .Net 2.0) can be declared static indicating it contains only static members.
    Plan? There ain't no plan!

  4. #4
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there the static class in C++?

    C# allows you to have static classes and all members inside the class must be static.
    C++ allows you to declare static members inside a class.
    After instantiating the class many times, all the static fields are going to have the same value in all instances.

  5. #5
    Join Date
    Jan 2006
    Location
    Winnipeg, MB. Canada
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there the static class in C++?

    Quote Originally Posted by probine
    C++ allows you to declare static members inside a class.
    After instantiating the class many times, all the static fields are going to have the same value in all instances.
    Of course a regular C# class or struct can contain static members just as in C++ with the same effect. The static class declaration is just sugar so the compiler can enforce no instanance members have been added to the class and that the class itself cannot be instantiated. You could accomplish the same thing using all static members and a private constructor.

    I only mentioned it in the first place because C# is the only language that i personally know of that actually has a declaration for a static class.
    Plan? There ain't no plan!

Similar Threads

  1. static enum in a class
    By mickey in forum General Programming
    Replies: 3
    Last Post: 19th August 2008, 20:02
  2. Replies: 16
    Last Post: 23rd May 2008, 11:12
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 08:57
  4. Replies: 2
    Last Post: 16th March 2007, 10:04
  5. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 11:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.