Results 1 to 4 of 4

Thread: static class members

  1. #1
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default static class members

    This is probably a noob question, but...

    When I create a static class member of an object type, for example:
    Qt Code:
    1. class A {
    2. static const A fixed = A();
    3. // should really be immutable, but const not necessary
    4. };
    To copy to clipboard, switch view to plain text mode 

    It produces an error "invalid in-class initialization of static data member of non-integral type" so i leave it as:
    Qt Code:
    1. class A {
    2. static A mFoo;
    3. // should really be immutable, but const not necessary
    4. };
    To copy to clipboard, switch view to plain text mode 
    hoping that A will be defined, but apparently it's not. If I leave it like this and try to define it in a function it complains "undefined reference to `A::mFoo'". Examples on the subject use integral data types so it works. I want it to be a fixed object. So how am I supposed to define it?? If I use a pointer and allocate memory the situation doesn't change.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: static class members

    Quote Originally Posted by space_otter View Post
    If I use a pointer and allocate memory the situation doesn't change.
    It will change, since that is possible (because the compilers does not have to know the size of A). A common case for that is the singleton pattern.
    Short and "bad":
    Qt Code:
    1. class Singleton
    2. {
    3. public:
    4. static Singleton* getInstance( );
    5. ~Singleton( );
    6. private:
    7. Singleton( ) {instance = new Singleton();}
    8. static Singleton* instance;
    9. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: static class members

    Maybe this will help?
    In your cpp, outside any function
    Qt Code:
    1. A* A::mFoo=NULL;
    To copy to clipboard, switch view to plain text mode 

    in header
    Qt Code:
    1. class A;
    2.  
    3. class A
    4. {
    5. ///...
    6. static A* mFoo;
    7. ///...
    8. };
    To copy to clipboard, switch view to plain text mode 
    I'm a rebel in the S.D.G.

  4. #4
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static class members

    Ok! I got this.

    Qt Code:
    1. class A {
    2. public:
    3. static const A* Foo()
    4. {
    5. if(mFoo == 0)
    6. {
    7. mFoo = new A();
    8. mFoo->clear();
    9. }
    10.  
    11. return mFoo;
    12. }
    13. private:
    14. static A*mFoo;
    15. };
    16.  
    17. // in cpp
    18. A *A::mFoo= 0;
    To copy to clipboard, switch view to plain text mode 
    works.

    Attempting to use
    Qt Code:
    1. A *A::mFoo = new A();
    To copy to clipboard, switch view to plain text mode 
    to simplify the code caused the most random errors I've seen.

Similar Threads

  1. for i = 0 to nr members in a class ...
    By qt_gotcha in forum Qt Programming
    Replies: 4
    Last Post: 6th March 2010, 19:38
  2. Class value members & references
    By agnus in forum General Programming
    Replies: 3
    Last Post: 6th December 2009, 14:18
  3. Question about class members
    By serenti in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2009, 14:20
  4. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37

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.