Results 1 to 5 of 5

Thread: Useless but curious compiler warning question

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Useless but curious compiler warning question

    Hi, here is a tea question:

    the warning in subject is "class::constname will be initialized after const type class::secondconstname when initialized here [the initialization row]"

    Situation:
    let's suppose that we declare two constant variables in a class
    Qt Code:
    1. class myCuriousClass{
    2. private:
    3. const int constant1;
    4. const int constant2;
    5. public:
    6. myCuriousClass();
    7. };
    To copy to clipboard, switch view to plain text mode 
    and then we valorize them as expected within a class: by the constructor
    Qt Code:
    1. myCuriousClass::myCuriousClass()
    2. :constant2(2),constant1(1)
    3. {
    4. //nothing smart to do here
    5. }
    To copy to clipboard, switch view to plain text mode 
    the fact that we initialized them in reversed order than we declared them makes the compiler complain
    myCuriousClass::constant1 will be initialized before const int myCuriousClass::constant2 when initialized here (row 3 of the second piece of code)
    So the question is, what is the problem if I reverse the order? Why was dedicated a warning to this situation?

    PS: for this test I used a MinGW compiler.

    Thank you for your attention
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  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: Useless but curious compiler warning question

    Imagine if there's a dependency between member variable initialization. Let's say member variable A is passed to the constructor of member variable B. Initializing them in wrong order might lead to passing an uninitialized variable.
    J-P Nurmi

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

    Raccoon29 (29th July 2008)

  4. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Useless but curious compiler warning question

    Just for this?
    Actually I believed there were deeper reasons
    Thank you anyway
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Useless but curious compiler warning question

    Quote Originally Posted by Raccoon29 View Post
    Actually I believed there were deeper reasons
    It's a warning, not an error.

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

    Default Re: Useless but curious compiler warning question

    The order you write suggest both to you and the readers of your code that the variables will be initialized in that order.
    The order, however, is defined by the declaration of the class, not the initializer list of a constructor implementation.

    So, if the order is not correct and the code might depend upon the order of initialization you get a warning so you can make sure there is no such problem.

Similar Threads

  1. Visual Studio 2005 Express
    By Muzz in forum Installation and Deployment
    Replies: 22
    Last Post: 6th November 2008, 07:21
  2. Crash handler on Win32
    By niko in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 20:41
  3. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 15:18
  4. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 22:41
  5. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 06:23

Tags for this Thread

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.