Results 1 to 4 of 4

Thread: enum initialization

  1. #1
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default enum initialization

    I have been programming with Qt for some time now and I have come across a non-Qt code snippet that uses the following:

    Qt Code:
    1. namespace MySpace {
    2. class MyClass;
    3. enum { SomeProperty = QTextFormat::UserProperty + 248 };
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    and is later reference by:

    Qt Code:
    1. namespace MySpace {
    2. MyClass::MyClass()
    3. {
    4. myFunction(MySpace::SomePropery, param2);
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    I am sure I should know what this is doing - but like a newbie ... I have no idea.

    Can someone explain?

    Environment:
    Linux Mint 17.2
    Qt Open Source 32 bit v5.5.1

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: enum initialization

    Qt Code:
    1. enum { SomeProperty = QTextFormat::UserProperty + 248 };
    To copy to clipboard, switch view to plain text mode 
    This creates an enum type with single value named
    Qt Code:
    1. SomeProperty
    To copy to clipboard, switch view to plain text mode 
    . You can assign whatever (integer) values you like to enum values.
    Qt Code:
    1. QTextFormat::UserProperty
    To copy to clipboard, switch view to plain text mode 
    is another enum value defined in QTextFormat namespace. It has an integer value, so you can use it as a number in arithmetic expression.
    The code above is roughly the same as
    Qt Code:
    1. class MyClass;
    2. static const int SomeProperty = QTextFormat::UserProperty + 248;
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 
    This
    Qt Code:
    1. MySpace::SomePropery
    To copy to clipboard, switch view to plain text mode 
    is how you can access the value of the enum. `ClassName` followed by double colon is a way to access the static member of a class.

  3. #3
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: enum initialization

    Well I said "I am sure I should know what this is doing - but like a newbie ... I have no idea."

    I get the enum has only one element and I also get that that element is using the value of (QTextFormat::UserProperty + 248) to create a user defined value;

    I guess what has me puzzled is why do this at all. Wouldn't it be just as well to make a static value = QTextFormat::UserProperty + 248 in a global or modular context? What is the advantage of using the enum construct if there is only one element?

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: enum initialization

    Almost none. Some old C++ compilers had no support for static const members. Using enums like this was a way around that limitation. This is known as "enum hack".

Similar Threads

  1. QDesktopWidget initialization
    By drweilert in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2011, 20:42
  2. A doubt about initialization of variables
    By vcp in forum Qt Programming
    Replies: 5
    Last Post: 11th December 2009, 11:42
  3. 'new' in initialization list
    By Kumosan in forum General Programming
    Replies: 5
    Last Post: 26th May 2008, 17:09
  4. static member initialization
    By high_flyer in forum General Programming
    Replies: 2
    Last Post: 17th September 2007, 22:22
  5. Initialization code
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 27th February 2007, 12:48

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.