Results 1 to 3 of 3

Thread: static member initialization

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default static member initialization

    Hi,

    I have the following case:
    I have a static member variable (NOT const!), that should manage unique information shared by all class instances, and I would like to initialize that variable one time.
    This is what I did:
    Header:
    Qt Code:
    1. class FrmPower : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. typedef enum { NONE=0,A,B,C,D } BUS;
    6. private:
    7. static BUS m_BorC;
    8. //rest of the class
    9. };
    To copy to clipboard, switch view to plain text mode 
    And in the implementation file (cpp) (in global scope):
    Qt Code:
    1. FrmPower::BUS m_BorC = FrmPower::NONE;
    To copy to clipboard, switch view to plain text mode 
    But I get multiple definition linking error for 'm_BorC' which I don't get, since it is not visible outside the implementation of FrmPower.

    Any idea how to resolve this?

    Thanks in advance.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: static member initialization

    Try:
    Qt Code:
    1. FrmPower::BUS FrmPower::m_BorC = FrmPower::NONE;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    high_flyer (17th September 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: static member initialization

    heh... thanks!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Problem emitting signal from a static function
    By Valheru in forum Qt Programming
    Replies: 21
    Last Post: 12th June 2007, 14:48
  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. Y is className a member function rather than a static one ?
    By sunil.thaha in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2007, 00:43

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.