Results 1 to 8 of 8

Thread: C++ Static Functions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ Static Functions

    Quote Originally Posted by calhal View Post
    It's not. Read what wysota wrote above.
    Strange, I just copied and pasted the code and it works fine here. Maybe it's my compiler thats broken

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ Static Functions

    From Thinking in C++

    Because static data has a single piece of storage regardless of how many objects are created, that storage must be defined in a single place. The compiler will not allocate storage for you. The linker will report an error if a static data member is declared but not defined.

    The definition must occur outside the class (no inlining is allowed), and only one definition is allowed. Thus, it is common to put it in the implementation file for the class. The syntax sometimes gives people trouble, but it is actually quite logical. For example, if you create a static data member inside a class like this:
    Qt Code:
    1. class A {
    2. static int i;
    3. public:
    4. //...
    5. };
    To copy to clipboard, switch view to plain text mode 
    Then you must define storage for that static data member in the definition file like this:
    Qt Code:
    1. int A::i = 1;
    To copy to clipboard, switch view to plain text mode 
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ Static Functions

    Yes, It makes sense, I'm just wondering why my compiler doesn't require it.

    And now I know why - it was optimising it out. If I do some more work with it, such as assign it to a volatile or such like, then it quite happily throws up a linker error.

    You may have noticed I've never used a c++ static member variable. Static functions a plenty, but never variables.

Similar Threads

  1. Replies: 11
    Last Post: 13th July 2009, 16:05
  2. Replies: 16
    Last Post: 23rd May 2008, 10:12
  3. Replies: 2
    Last Post: 16th March 2007, 09:04
  4. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10:50
  5. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00

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
  •  
Qt is a trademark of The Qt Company.