Jump to content
  • Advertisement
Sign in to follow this  
Dark Rain

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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;
};



Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!