Breaking cyclic dependency in C?

Class my_class; typedef std::map my_map; ~~~~~~~~~~ use pointer here!

Please do not use pointers. This will just lead to leaks as you are not explicitly defining the ownership of the objects. – Loki Astari Jul 28 '10 at 14:05 +1, though this assumes you don't want adding this to the map to cause a copy.

That's generally true, though. @izex, do you really need intend to create a full copy of the object every time you put it in the map or read it from the map? That typically is quite expensive.As an aside, I've grown to really like shared_ptr for this problem: typedef std::map > my_map; It just cleans up the memory management headache of using a raw pointer here.

– Rob Napier Jul 28 '10 at 14:09.

Circular dependencies are generally a Bad Thing. Can you re-think your design a bit? Does a my_class really need to know what its container is?

If that's not practical, instead of an iterator into my_map, could you get away with just a my_container*? The compiler won't have a problem with using an incomplete type for the pointer.

Thanks. I know it is not trivial, but in my case the circular dependency is necessary. I solved the problem usin a my_container* indeed.

– izex Jul 28 '10 at 23:30.

Put my_map as a member in my_class, like this: class my_class { public: typedef std::map my_map; ... private: my_map::iterator iter; }; class otherclass{ public: my_class::my_map::iterator getIter(); private: my_class::my_map map; }; If you don't want to always use my_class::, then make another typedef.

You could put the typedef inside my_class: class my_class { public: typedef std::map my_map; ... private: my_map::iterator iter; }; class otherclass{ public: my_class::my_map::iterator getIter(); private: my_class::my_map map; }.

Yep, this should work, but unfortunately I made a mistake formatting my question, and my situation is a bit more complicated. Please see post. – izex Jul 28 '10 at 9:18.

How about: #include class my_class; // forward declare the type. Typedef std::map my_map; class my_class { private: my_map::iterator iter; }; class otherclass { public: my_map::iterator getIter(); private: my_map map; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions