By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,442 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,442 IT Pros & Developers. It's quick & easy.

Attempting to understand static class properties...

P: n/a
Hello everyone,

I am having a problem with static class properties that I suspect is somewhat
trivial, but I don't understand what is wrong. I am using GNU C++ on
Windows, though I am (hoping) to make this program cross-platform using the
wxWidgets library and not using any functionality that isn't commonly
supported. Anyway, the problem is that I want to have a class that has two
variables and two methods that operate on the class itself, and not an
instance of it. Here is the class declaration:

class TmudConfiguration {
private:
static bool InitState;
static TmudConfiguration *inst;
public:
TmudConfiguration();
};

I have this in the .h file for the class. In the .cc file for the class, I
have code that refers to all of these things, but every time the linker finds
a reference to InitState or TmudConfiguration, it tells me that it is an
"undefined reference to TmudConfiguration::InitState" or the other. A snippet:

TmudConfiguration::TmudConfiguration() {
if(TmudConfiguration::InitState == true) {
delete this;
} else {
TmudConfiguration::InitState == true;
}
}

I don't quite understand why that doesn't work. If I remove the
TmudConfiguration:: prefix from InitState, it does the same thing. Examples
that I've looked at from the Internet seem to show that the variable just gets
accessed as if it were a locally scoped variable within a function and they work.

Interestingly enough, though, I just tried to compile an example that did not
work, either (obtained from
http://www.cprogramming.com/tutorial...ckeyword.html). Though, I can't
find anything in what I am doing that differs from that or any other example,
really. :-/

Can someone help me to understand what I am doing wrong?

-- Mike
Dec 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <Ee******************************@comcast.com>,
"Michael B. Trausch" <mi*************@comcast.netwrote:
Hello everyone,

I am having a problem with static class properties that I suspect is somewhat
trivial, but I don't understand what is wrong. I am using GNU C++ on
Windows, though I am (hoping) to make this program cross-platform using the
wxWidgets library and not using any functionality that isn't commonly
supported. Anyway, the problem is that I want to have a class that has two
variables and two methods that operate on the class itself, and not an
instance of it. Here is the class declaration:

class TmudConfiguration {
private:
static bool InitState;
static TmudConfiguration *inst;
public:
TmudConfiguration();
};

I have this in the .h file for the class. In the .cc file for the class, I
have code that refers to all of these things, but every time the linker finds
a reference to InitState or TmudConfiguration, it tells me that it is an
"undefined reference to TmudConfiguration::InitState" or the other.
In the .cc file, after the #includes, put in the following lines:

bool TmudConfiguration::initState = false;
TmudConfiguration* TmudConfiguration::inst = 0;
Dec 23 '06 #2

P: n/a
"Michael B. Trausch" <mi*************@comcast.netwrote in message
news:Ee******************************@comcast.com
Hello everyone,

I am having a problem with static class properties that I suspect is
somewhat trivial, but I don't understand what is wrong. I am using
GNU C++ on Windows, though I am (hoping) to make this program
cross-platform
using the wxWidgets library and not using any functionality that
isn't commonly supported. Anyway, the problem is that I want to have a
class that
has two variables and two methods that operate on the class itself,
and not an instance of it. Here is the class declaration:

class TmudConfiguration {
private:
static bool InitState;
static TmudConfiguration *inst;
public:
TmudConfiguration();
};

I have this in the .h file for the class. In the .cc file for the
class, I have code that refers to all of these things, but every time the
linker finds a reference to InitState or TmudConfiguration, it tells me
that it is
an "undefined reference to TmudConfiguration::InitState" or the
other. A snippet:
Static variables are like functions: you declare them in the class and you
define them outside the class. Add:

bool TmudConfiguration::InitState;

and

TmudConfiguration *TmudConfiguration::inst;

to your .cc file, NOT the header file. Note that you don't use the static
keyword in the .cc file.
--
John Carson
Dec 23 '06 #3

P: n/a
Michael B. Trausch wrote:
Hello everyone,

I am having a problem with static class properties that I suspect is
somewhat trivial, but I don't understand what is wrong. I am using GNU
C++ on Windows, though I am (hoping) to make this program cross-platform
using the wxWidgets library and not using any functionality that isn't
commonly supported. Anyway, the problem is that I want to have a class
that has two variables and two methods that operate on the class itself,
and not an instance of it. Here is the class declaration:

class TmudConfiguration {
private:
static bool InitState;
static TmudConfiguration *inst;
public:
TmudConfiguration();
};
http://www.parashift.com/c++-faq-lit...html#faq-10.11
Dec 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.