# operator= and heritage

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
Quote:
 Original post by Dark RainI 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.

    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;    };

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by Dark RainYeah, 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 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.

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633343
• Total Posts
3011434
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!