Results 1 to 6 of 6

Thread: const member definitions in a class ....

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default const member definitions in a class ....

    Sorry,
    I know this question has been asked many a time but what is the "standard" work around for defining a consts (and const arrays) in a C++ class??? That is, is there a nice way of doing it? What's the best and most readable way of doing it?

    it's horrible:

    Qt Code:
    1. class MyClass
    2. {
    3.  
    4. const int arraySize = 3;//won't work
    5. const int arr[3] = {1, 2, 3}; //won't work
    6.  
    7. enum{
    8. arraySize = 3;
    9. };
    10. //but I still can't ...
    11. const int arr[arraySize] = {1, 2, 3};
    12.  
    13. //see, I've got stuff that's really natural in a class definition - rather than implementation
    14. //thus:
    15. enum RANGE {
    16. _OPEN = 0,
    17. _2R,
    18. _20R,
    19. _200R,
    20. _2K,
    21. _20K,
    22. _200K,
    23. _2M
    24. };
    25. const double _RAN[_2M+1] = {
    26. 0,
    27. 2,
    28. 20,
    29. 200,
    30. 2000,
    31. 20000,
    32. 200000,
    33. 2000000
    34. };
    35. //so that in my code I can say things like setRange(_RAN[_2M]);
    36. }
    To copy to clipboard, switch view to plain text mode 

    thanks
    Kev

  2. #2
    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: const member definitions in a class ....

    just do:
    Qt Code:
    1. class MyClass : pulic ParentClass
    2. {
    3. const int arraySize
    4. ....
    5. }
    6.  
    7. MyClass::MyClass():ParentClass(),arraySize(3)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: const member definitions in a class ....

    Won't that get messy with a const array as in my example?
    K

  4. #4
    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: const member definitions in a class ....

    Oh right, you have a const array too...
    Hmm...
    I found this on google.

  5. #5
    Join Date
    Jan 2006
    Location
    New Zealand
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: const member definitions in a class ....

    Declare in your class:

    static const int arr[];


    and in your cpp file:

    const int MyClass::arr[] = { 1, 2, 3 };

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: const member definitions in a class ....

    Ha!
    salubrious, dude!
    that'll do, nicely.

    thanks
    K

Similar Threads

  1. shared vs static
    By alisami in forum Installation and Deployment
    Replies: 3
    Last Post: 4th October 2008, 14:04
  2. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 08:57
  3. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 18:04
  4. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 08:43
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42

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.