Results 1 to 6 of 6

Thread: accessing static member variables of one class in another class

  1. #1
    Join Date
    Mar 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb accessing static member variables of one class in another class

    Doh! I have been hammering my head against the wall on this off and on, searching for a solution and none of them worked, finally, 5 minutes after posting this, I tried adding the const keyword and initializing the variables in the header file, and removing the inits from the cpp and it worked...

    I maintain I never would have figured it out if I hadn't posted this...It's the general law of the universe.
    Hi,

    This is more or less a c++ question only, so I am posting it here. It is, in my mind something that should be simple, I am just missing the point.

    I have tried it fifty different ways and none of them work. The problem:

    I need to access global constants in any part of my program. These are basically just message names that I pass around where the value is just an Int.

    What I want it to look like is: Messages::SomeMessageName. This is done alot in say AS3 or PHP, where you define a class that holds a bunch of static variables so you can access them like if (msg == Message::SomeMessageName) ...

    I have tried using just a namespace, I have tried creating a class, I have tried having QtCreator generate me a class based on qObject etc, and Always, I get this error:

    Qt Code:
    1. WhateverClass.cpp:47: undefined reference to `Messages::PluginManagerInitComplete'
    To copy to clipboard, switch view to plain text mode 

    Here is the class code:

    Qt Code:
    1. Messages.h
    2. ----------------
    3. #ifndef MESSAGES_H
    4. #define MESSAGES_H
    5. class Messages
    6. {
    7. public:
    8. Messages();
    9. static int WebViewCreated;
    10. static int WebViewShown;
    11. static int LinkClicked;
    12. static int JavaScriptCleared;
    13. static int PluginManagerInitComplete;
    14. static int PluginManagerUnload;
    15.  
    16. };
    17.  
    18. #endif // MESSAGES_H
    To copy to clipboard, switch view to plain text mode 

    And:

    Qt Code:
    1. Messages.cpp
    2. -----------------
    3. #include "Messages.h"
    4. int Messages::WebViewCreated = 1;
    5. int Messages::WebViewShown = 2;
    6. int Messages::LinkClicked = 3;
    7. int Messages::JavaScriptCleared = 4;
    8. int Messages::PluginManagerInitComplete = 5;
    9. int Messages::PluginManagerUnload = 6;
    10. Messages::Messages()
    11. {
    12. }
    To copy to clipboard, switch view to plain text mode 

    This is probably not how I want to do it, I just tried this and it fails, always the same message, undefined reference. This is how-ish (not exactly) be done is say AS3 or something, but this is obviously not the C++ way, what I am hoping to learn is, how will I go about this is C++?

    Many thanks,
    Jason
    Last edited by jasonknight; 11th June 2010 at 18:18. Reason: stupidity

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: accessing static member variables of one class in another class

    You should create static functions.

    Example:
    Qt Code:
    1. class Messages
    2. {
    3. Messages();
    4.  
    5. static int webViewCreated();
    6. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Messages::Messages()
    2. {
    3. }
    4.  
    5. int Messages::webViewCreated()
    6. {
    7. return 1;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. if (something == Messages::webViewCreated()) {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 

    I usually do this kind of thing via flags (enums) though.

  3. #3
    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: accessing static member variables of one class in another class

    I'd say you forgot to include Messages.h in the other file but then you should either make the variables const int instead of static (unless you feel like modifying their values at runtime) or better yet enum values as suggested.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4

    Exclamation Re: accessing static member variables of one class in another class

    Quote Originally Posted by jasonknight View Post
    Doh! I have been hammering my head against the wall on this off and on, searching for a solution and none of them worked, finally, 5 minutes after posting this, I tried adding the const keyword and initializing the variables in the header file, and removing the inits from the cpp and it worked...

    I maintain I never would have figured it out if I hadn't posted this...It's the general law of the universe.
    I am developing a Qt mobile application and was hit with the same type of problem. However, in my case it is unable to get around the errors with the trick of "const keyword & initialization in header file", since most of my static member variables are classes (QSize), and one static member variable is even a QVector<QSize> that is shared by other part of the program.

    I suppose this is quite Qt-specific. I've been long using similar code snippets under Visual C++ and have got no problems at all. Now I was really driven mad as I've spent quite a lot of time on it and not come up with a solution under Qt! Or could it be due to the application being for mobile phones, thus actually this is the limitation of Qt 4 Symbian?

  5. #5

    Default Re: accessing static member variables of one class in another class

    Quote Originally Posted by tbscope View Post
    You should create static functions.
    Unfortunately, changing to static functions does NOT seem to work...

    Even if it would work, it wouldn't help solve my case: I need a global static QVector<QSize> variable, the initialization class assgins its value and then it is share-used among other classes thus could be modified (e.g. append() an element).

  6. #6
    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: accessing static member variables of one class in another class

    Quote Originally Posted by adtbug View Post
    I suppose this is quite Qt-specific. I've been long using similar code snippets under Visual C++ and have got no problems at all.
    I have a really short and simple answer to such statements: Qt is C++.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Using a static member in a class
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 29th April 2010, 10:58
  2. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  3. QDevelop debuggng - viewing class member variables
    By dbrmik in forum Qt-based Software
    Replies: 0
    Last Post: 7th January 2009, 10:40
  4. Replies: 3
    Last Post: 19th February 2008, 13:10
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.