initializing static stdlib containers

This is a discussion on initializing static stdlib containers within the C++ Programming forums, part of the General Programming Boards category; If I wanted to count how many objects of a class I make, I could write Code: // header.hh class ...

  1. #1
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31

    initializing static stdlib containers

    If I wanted to count how many objects of a class I make, I could write
    Code:
    // header.hh
    
    class bob
        {
        public:
            bob() ;
        private:
            static int bobCount_ ;
        }
    and then a file with the code and initialization

    Code:
    // code.cc
    
    #include "header.hh"
    
    bob::bobCount_ = 0 ;
    
    bob::bob()
        {
        bobCount_++ ;
        }
    So here is the question. Say I want to instead add each object to a stdlib set? Then my header file is

    Code:
    // header.hh
    
    #include <set>
    
    class bob
        {
        public:
            bob() ;
        private:
            static std::set<bob*> bobSet ;
        }
    and then a file with the code and initialization

    Code:
    // code.cc
    
    #include "header.hh"
    
    bob::bobSet = ????
    
    bob::bob()
        {
        bobSet.insert( this ) ;
        }
    So how do I initialize the set container class? The row of question marks doesn't work!

    Brian

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    You can just write in code.cc:
    Code:
    std::set<bob*> bob::bobSet;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    Claremont, CA
    Posts
    31

    Thanks!

    Thanks, that definitely works. I never would have come up with the syntax, although now that I've seen it, I understand where it comes from.

    Brian

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, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 06: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. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM

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