Thread: static members construction

  1. #1
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345

    Question static members construction

    are static members in a class garanteed to be constructed before any instance of the class?
    I have a class that adds a pointer to each instance to a static std::list on construction, but I'm getting an access violation error when creating an instance of this class, so can this be the reason?
    Any suggestions?
    TIA

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    yes, static members are created when the program is first run like globals. Can u just post some of your code?

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    weird, it kept giving access violation when trying to access the static member, I fixed it this way:
    Code:
    std::list& MyClass::getStaticMember() {
        static std::list myList;
        return myList;
    }

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    can u post ur original class definition?

    if the static data member is private u can only access it once outside the class, thats for initialising the memeber.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM