Attempting to understand static class properties...

M

Michael B. Trausch

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/statickeyword.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
 
J

John Carson

Michael B. Trausch said:
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.
 
D

Daniel T.

"Michael B. Trausch said:
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;
 
R

red floyd

Michael said:
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-lite/ctors.html#faq-10.11
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top