Attempting to understand static class properties...

Discussion in 'C++' started by Michael B. Trausch, Dec 23, 2006.

  1. 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
     
    Michael B. Trausch, Dec 23, 2006
    #1
    1. Advertising

  2. Michael B. Trausch

    John Carson Guest

    "Michael B. Trausch" <> wrote in message
    news:
    > 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
     
    John Carson, Dec 23, 2006
    #2
    1. Advertising

  3. Michael B. Trausch

    Daniel T. Guest

    In article <>,
    "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();
    > };
    >
    > 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;
     
    Daniel T., Dec 23, 2006
    #3
  4. Michael B. Trausch

    red floyd Guest

    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-lite/ctors.html#faq-10.11
     
    red floyd, Dec 23, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,514
    Tony Morris
    Oct 19, 2004
  2. bugbear
    Replies:
    4
    Views:
    466
    bugbear
    Aug 23, 2006
  3. Robin
    Replies:
    0
    Views:
    439
    Robin
    Jun 6, 2007
  4. SuperZE
    Replies:
    5
    Views:
    362
    SuperZE
    Oct 6, 2008
  5. yawnmoth
    Replies:
    3
    Views:
    97
    Thomas 'PointedEars' Lahn
    Jan 14, 2008
Loading...

Share This Page