PDA

View Full Version : accessing static member variables of one class in another class



jasonknight
11th June 2010, 18:09
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:


WhateverClass.cpp:47: undefined reference to `Messages::PluginManagerInitComplete'

Here is the class code:



Messages.h
----------------
#ifndef MESSAGES_H
#define MESSAGES_H
class Messages
{
public:
Messages();
static int WebViewCreated;
static int WebViewShown;
static int LinkClicked;
static int JavaScriptCleared;
static int PluginManagerInitComplete;
static int PluginManagerUnload;

};

#endif // MESSAGES_H


And:



Messages.cpp
-----------------
#include "Messages.h"
int Messages::WebViewCreated = 1;
int Messages::WebViewShown = 2;
int Messages::LinkClicked = 3;
int Messages::JavaScriptCleared = 4;
int Messages::PluginManagerInitComplete = 5;
int Messages::PluginManagerUnload = 6;
Messages::Messages()
{
}


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

tbscope
11th June 2010, 20:37
You should create static functions.

Example:


class Messages
{
Messages();

static int webViewCreated();
};




Messages::Messages()
{
}

int Messages::webViewCreated()
{
return 1;
}




if (something == Messages::webViewCreated()) {
...
}


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

wysota
11th June 2010, 21:40
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.

adtbug
6th September 2010, 12:23
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?

adtbug
6th September 2010, 12:40
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).

wysota
6th September 2010, 14:53
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++.