Jump to content
  • Advertisement
Sign in to follow this  
Aliii

Assignment operator and virtual inheritance

This topic is 1417 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

With virtual inheritance you can prevent a base class from being created more than once. So far I couldnt find a proper way to prevent the assignment/move-assignment operator from being called multiple times on the base. As for C++11 or 14, is there some new feature that makes this possible? If not, whats the best way to handle this? (apart from not putting any data into the base:)) Thanks!

Share this post


Link to post
Share on other sites
Advertisement

You have the "dreaded diamond" hierarchy. You have virtual inheritance.

A
| \
|   \
B    C
|   /
| /
D

D d1;    //A`s constructor will be called once. Good
D d2;

d1 = d2; //D will call B`s and C`s operator=, and those both will call A`s. ....so it will be called twice.

Also, with move assignment, ...you are supposed to zero out the data in the source after you`ve copied it. So thats no good.

 

 

Share this post


Link to post
Share on other sites

@Alvaro - Thanks, I eventually realized that - I was not familiar with virtual inheritance (I seldom use inheritance outside of GUIs and never use multiple inheritance except for the rare case of multiple interfaces)

 

The only solution I can think of is to override assignment yourself to ensure it does what you want, though you seem to be looking for a built in language feature.

Edited by achild

Share this post


Link to post
Share on other sites

I do have a class: CObjectWithID. If you derive from it:

class CA : public CObjectWithID{ ....

class CB : public CObjectWithID{ ....

...it will automatically generate and maintain ID-s for objects of CA and CB. It uses the constructors, destructor, assignment operatorts for that. I dont wanna go too much into it, but the point is it works as long as you dont do something like this: class CA : public CB, public CObjectWithID{...

I will find a workaround but It made me wonder about this problem with the assignment operators and virtual inheritance.

Share this post


Link to post
Share on other sites

Other than efficiency, why would it cause a problem to call the virtual base assignment operator twice, unless the virtual base class does not implement copy semantics?  If the only problem is efficiency, perhaps you should look at using a lighter-weight data representation.

Share this post


Link to post
Share on other sites

Just wagering a guess here - but it looks like he has an ID system which gives new IDs to objects upon assignment. In this case 2 IDs would be generated... despite the number of design questions it raises, it's hard to tell with what we've been given if that is problematic or just annoying.

Edited by achild

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!