stl map ... what do i need to make this work

Started by
3 comments, last by me22 18 years, 4 months ago
Ive tried many different things but can figure out what i need to make this work // I HAVE THIS Stats & operator = (Stats & rStats); do i really overload it more for this case?

// i have this overload
Stats & operator = (Stats & rStats);
//
// This is from Stats::Load()
// search the map to see if already loaded
std::map<std::string, Stats>::iterator it;
it = m_statMap.find(fileName);
if(it != m_statMap.end())
{
	*this = it->second;  // or   this = it->second // or this = *it->second // none work
	return true;
}



I know what the errors mean but i just think that i should be able to dereference it in such a way i dont need to overload more.. error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Stats *const ' (or there is no acceptable conversion) thanks
Advertisement
replying to my own again ...huh

i figured out what was wrong with the above.. but now let me ask a different Q? about std::Map

if you make a map like previous post
std::map<std::string, Stats>

it needs to call the default constructor so it can allocate its self..

but what happends when you assign something in to the allocated spot.. does it call = or does it do something else...

the reason i ask is because for me the = does not copy altheway up the inheritance line .... not to ?menchon? i dont use default constructors
A wild guess and without checking:
IF some object is already existing in the map it will assign a value using =. If the object does NOT exist, it will create one using the copy constructor. Hence, to be on the safe side you should always do both.
f@dzhttp://festini.device-zero.de
Quote:Original post by MTclip
the reason i ask is because for me the = does not copy altheway up the inheritance line ....

If this is happening it is likely you are providing user-defined copy-assignment operators and not calling the parent copy-assignment operator. For example this code:
class Base{	public:		Base(int base)			:			base(base)		{		}	private:		int base;};class Derived	:	public Base{	public:		Derived(int base, int derived)			:			Base(base),			derived(derived)		{		}		Derived & operator=(Derived const & d)		{			derived = d.derived;			return *this;		}	private:		int derived;};int main(){	Derived d1(1, 2);	Derived d2(7, 8);	d1 = d2;}

leaves d1 with values d1.base = 1 and d1.derived = 8. Derived::operator= probably should have been implemented as:
Derived & operator=(Derived const & d){	Base::operator=(d); // call Base copy assignment operator	derived = d.derived;	return *this;}

Which would have left d1 with values d1.base = 7 and d1.derived = 8.

Enigma
Quote:Original post by Trienco
A wild guess and without checking:
IF some object is already existing in the map it will assign a value using =. If the object does NOT exist, it will create one using the copy constructor. Hence, to be on the safe side you should always do both.


In fact, std containers require that the types in them are copy-constructible and assignable, with value semantics.

Also, remember that you won't get polymorphic behavior with a std::vector<Base>, for example--you need pointers or references to get polymorphism. std::vector< std::tr1::shared_ptr<Base> > or boost::ptr_vector<Base> is what you actually want.

This topic is closed to new replies.

Advertisement