Results 1 to 2 of 2

Thread: Outside Main

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Outside Main

    I'm trying to understand why you can't do something like this:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QDebug>
    3.  
    4. //Global variable
    5. int value(1);
    6.  
    7. qDebug() << value; //request for global variable
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QCoreApplication a(argc, argv);
    12.  
    13. int value(2); //local variable
    14.  
    15. qDebug() << value; //request for local variable
    16. qDebug() << ::value; //request for global variable
    17.  
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    I read that this is the way C++ was designed which is fair enough, I'm just trying to understand why it was done this way. Is it a Windows limitation? I'm guessing not. I'm not really familiar with scripting languages. I did pearl a long time ago for a class but that was it, don't remember anything about it. So Perl works under Windows doesn't it?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Outside Main

    Line 7 doesn't work because qDebug() << is code that needs to be executed, so it needs to be on an execution code path.

    For the variable name hiding: usually it is a bad idea to have local names that hide names on a more wider scope, for global or member variables that is usually solved by marking them explicitly as such.

    E.g.
    Qt Code:
    1. class Foo {
    2. private:
    3. int m_value; // using m_ prefix to say this is a member variable
    4. };
    To copy to clipboard, switch view to plain text mode 

    If you really need a global variable with such a common names as "value" you can put it into a namespace, e.g.

    Qt Code:
    1. namespace {
    2. int value; // access through ::value
    3. }
    4. namespace globalvalues {
    5. int value; // access through globalvalues::value
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 8th August 2014, 19:08
  2. Getting main app process
    By dcypher in forum Qt Programming
    Replies: 2
    Last Post: 2nd April 2013, 21:54
  3. main.moc
    By talk2amulya in forum Qt Programming
    Replies: 8
    Last Post: 19th February 2009, 07:38
  4. Replies: 11
    Last Post: 11th August 2008, 09:14
  5. Replies: 15
    Last Post: 23rd March 2007, 16:16

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.