c++ - Unusable derived class std::map member -
please observe following code:
class foo
class foo: { public: foo(); ~foo(); } class c
class c { public: c(); ~c(); void seta(a* a) { m_a = a; } a* geta() { return m_a; } private: a* m_a; } abstract class a:
class { public: // constructor, destructor (c* c) : parent (c) virtual ~a(); // relevant pure abstract methods void addfoo(foo* f, std::string& id) = 0; foo* getfoo(std::string id)= 0; protected: c* parent; std::map< std::string, foo* > m_foos; }; in b.h
class b : public { public: // constructor, destructor ... b(c* c); virtual ~b(); // definition of pure relevant methods virtual void addfoo(foo* f, std::string& id); virtual foo* getfoo(std::string id); }; in b.cpp
// methods implementation ... b::b(c* c) : a(c) { } foo* b::getfoo(std::string id) { return m_foos[id]; } b::setfoo( foo* f, std::string& id ) { m_foos[id] = f; } and then, in different part of code running smoothly until decided make m_foos inheritable:
c* c = new c(); c->seta(new b(c)); this executed beyond doubt before:
foo* f = new foo(); ... c->geta()->setfoo(f); program crashes when trying access m_foos inside setfoo. reason can't yet understand, m_foos not correctly initialized in b (running m_foos.size() instance returns crazy values). mind error @ runtime, not @ compilation time.
edit: weird, since adding protected member variable of type int in class allows modification , reading in b::setfoo()...
my psychic debugging skills tell me b either null pointer, hasn't actually been constructed, or prematurely destructed.
did accidentally create on stack , store pointer it?
edit: given adding member a changes behavior, somewhere in code you're getting undefined behavior. need see more of code more general guesses.
Comments
Post a Comment