static members construction

This is a discussion on static members construction within the C++ Programming forums, part of the General Programming Boards category; are static members in a class garanteed to be constructed before any instance of the class? I have a class ...

  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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21