Results 1 to 8 of 8

Thread: How do you do static constants right?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How do you do static constants right?

    I'm just not getting this right somehow. I would like to define a couple of static constants for a class, just like the static final variables in java. This is how I did it:

    Qt Code:
    1. class SerialCommunication
    2. {
    3. private:
    4. static const qint64 RX_BUFFER_SIZE = 128;
    5. };
    6.  
    7. void SerialCommunication::receiveMsg()
    8. {
    9. char receiveBuffer[RX_BUFFER_SIZE];
    10. int bytesRead = port->read(receiveBuffer, qMin(port->bytesAvailable(), RX_BUFFER_SIZE));
    11. }
    To copy to clipboard, switch view to plain text mode 

    For the second call to RX_BUFFER_SIZE in the read() command I get a compiler error "undefined reference to SerialCommunication::RX_BUFFER_SIZE". How come the first one is ok then? What am I not doing right?

  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: How do you do static constants right?

    Qt Code:
    1. class SerialCommunication
    2. {
    3. private:
    4. static const qint64 RX_BUFFER_SIZE;
    5. };
    6.  
    7. const qint64 SerialCommunication::RX_BUFFER_SIZE = 128;
    8.  
    9. void SerialCommunication::receiveMsg()
    10. {
    11. char receiveBuffer[RX_BUFFER_SIZE];
    12. int bytesRead = port->read(receiveBuffer, qMin(port->bytesAvailable(), RX_BUFFER_SIZE));
    13. }
    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:

    Cruz (5th February 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do you do static constants right?

    Kinda sucks though that you have to declare and initialize in two different places.

  5. #4
    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: How do you do static constants right?

    Or you can just define it outside SerialCommunication in the .cpp file.
    J-P Nurmi

  6. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do you do static constants right?

    You mean...global variables that are not members of the class? *scared*

  7. #6
    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: How do you do static constants right?

    But if you make it static, it's only visible inside the compilation unit.
    J-P Nurmi

  8. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do you do static constants right?

    or use an enum:
    Qt Code:
    1. class SerialCommunication
    2. {
    3. private:
    4. enum { RX_BUFFER_SIZE = 128 };
    5. };
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to caduel for this useful post:

    Cruz (6th February 2009)

  10. #8
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do you do static constants right?

    Quote Originally Posted by Cruz View Post
    Kinda sucks though that you have to declare and initialize in two different places.
    For integral types you don't have to: in-class initialization of const static data members of integral types. I don't know why your first example didn't work.
    The Wheel weaves as the Wheel wills.

Similar Threads

  1. How to use static mysql plugin
    By khikho in forum Qt Programming
    Replies: 7
    Last Post: 19th January 2009, 21:44
  2. Replies: 22
    Last Post: 8th October 2008, 13:54
  3. new problem with simple C++ static variables
    By landonmkelsey in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2008, 05:42
  4. Replies: 16
    Last Post: 23rd May 2008, 10:12
  5. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10: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
  •  
Qt is a trademark of The Qt Company.