This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++ std::map problem


Hello,

please cc me.

I'm having trouble making this snip of code work. I just want to keep a map of created objects (for (un)marshalling), but when I run the attached code, I see that the constructor is only called once, giving an output of 0111.

Now if I make this change:
-    static const toto* create() { return &totos[nextid]; }
+    static const toto* create() { toto(); return &totos[nextid]; }
I get 0246, which is what is expected, but not the initial objective.

Am I doing something wrong? Is there a bug in g++ or libstdc++?
I get the problem with both 3.3.2 20030812 and 2.95.4 20011002 (Debian sid)
I don't know what kind of keywords I could use to search, so I have to post.

Thank you,
#include <iostream>
#include <map>

class toto {
	toto():id(nextid++){}
	const unsigned int id;
	friend class std::map<unsigned int, const toto>;
public: // for debug purposes
	static std::map<unsigned int, const toto> totos;
	static unsigned int nextid;
public:
	static const toto* create() { return &totos[nextid]; }
};

unsigned int toto::nextid(0);
std::map<unsigned int, const toto> toto::totos;

int main(int argc, char* argv[]) {
	std::cout << toto::nextid;
	toto::create();
	std::cout << toto::nextid;
	toto::create();
	std::cout << toto::nextid;
	toto::create();
	std::cout << toto::nextid << std::endl;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]