Assignment operator and virtual inheritance

Started by
13 comments, last by SmkViper 9 years, 7 months ago

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!

Advertisement

Multiple times in a single invocation?

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.

[edit] I see - scratch my reply. You were talking about virtual inheritance.

I believe the original question makes perfect sense, but I just don't know the answer (I personally stay away from any form of inheritance that is not public single inheritance).

To achild: Are you familiar with virtual inheritance and how it's supposed to solve the "diamond problem"? If B and C inherit from A virtually, the diagram is most definitely what Aliii posted, and A's constructor is called only once. However, the assignment operator would be called twice.

@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.

Out of curiosity, do you have a use case for this or is it purely theoretical?

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.

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.

Stephen M. Webb
Professional Free Software Developer

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.

This topic is closed to new replies.

Advertisement