operator= and heritage

Started by
3 comments, last by Shannon Barber 18 years, 8 months ago
Hello, I've a problem with the operator=. I'm working with a very large legacy code base and there's a pretty big class hierarche that's based on the fact that the default copy operator is going to work, this means no pointers of course. Now, I need to add a new attribute one of the class, pretty much in the middle of the hierarchy and that attribute has to be a pointer since it's an interface to a virtual class. I've defined the operator= for that class and made my change. It just doesnt work and I'm not too sure in what "direction" is problem. Would I need to define the operator= for all the parent classes or all the base classes etc. Anyway, that's pretty much out of the question since it's a very large hierarche with a lot of attribute in each class etc. I tried to do something like this : Hull& Hull::operator=( const Hull& param ) { *this = param; m_Volume = new BoundingVolume( param.m_Volume ); return *this; } I thought I could just cheat this way but it bombs out with no error message at all, no core dump etc. It would be acceptable I suppose to go through 25 classes to implement this simple cheat function but I can't get it to work.
Advertisement
Quote:Original post by Dark Rain

I tried to do something like this :
Hull& Hull::operator=( const Hull& param ){	*this = param;		m_Volume = new BoundingVolume( param.m_Volume );	return *this;} 

That won't work of course because the function calls itself. Also, "m_Volume = new BoundingVolume( param.m_Volume )" is wrong.

In the assignment operator, you must explicitly assign each member variable and also call the assignment operator for the base class. You don't need to worry about writing assignment operators for base and derived classes and member variables since the default assignment operators already work.

Remember that if you provide an assignment operator, you probably need to provide a copy constructor too.

This example might help you:
    struct A    {        int a;    };    struct B : public A    {        int b;        Foo * p;        B() : b(0), p( new Foo ) {}        B( B const & source )        {            b = source.b;            p = new Foo( *source.p );        }        ~B()        {            delete p;        }        B & operator =( B const & rhs )        {            A::operator=( rhs );            if ( this != &rhs )            {                b = rhs.b;                delete p;                p = new Foo( *rhs.p )            }            return *this;        }    };    struct C : public B    {        int c;    }; 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Yeah, I know the line "new BoundingVolume( param.m_Volume );" is wrong, I just typed it out of memory. I'm actually using the clone pattern.

I don't need the copy constructor tho, because the class is created exactly twice for each hull. Once when reading fromt he file and a second time when doing an assignement to a map that looks something like that: std::map<long, item> m_items;

Anyway, I know it's crashing in that particular assignement operator since the server won't crash as long as there's no collision with an object using the new bounding volumes and there's exactly one so far. If I don't use the operator= everything works fine until that pointer is called but if I use it, it crash.

At any rate, thanks a lot for the info, I didn't know that calling "A::operator=( rhs );" would work if I hadn't defined it in the base class.

I'd tried assigning each member variable by hand and it didn'T crash but the whole object didn't get copied either, so that'S where I was starting to suspect I had to create operator= for the whole hierarchy, starting with my base class. I was panicking a bit, there's like 10 classes above me and they have 20 members each.
Or you could just wrap your pointer in a class which has a correctly implemented copy constructor and copy assignment operator, like boost::shared_ptr.

Enigma
Quote:Original post by Dark Rain
Yeah, I know the line "new BoundingVolume( param.m_Volume );" is wrong, I just typed it out of memory. I'm actually using the clone pattern.

I don't need the copy constructor tho, because the class is created exactly twice for each hull. Once when reading fromt he file and a second time when doing an assignement to a map that looks something like that: std::map<long, item> m_items;


If you write an overload for operator= you need to write a copy ctor. The program is seriously bugged if you do not.

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement