Victor Bazarov wrote:[color=blue]
>
anongroupaccount@googlemail.com wrote:[color=green]
> > Victor Bazarov wrote:[color=darkred]
> >>
anongroupaccount@googlemail.com wrote:
> >>> Victor Bazarov wrote:
> >>>
> >>>>
anongroupaccount@googlemail.com wrote:
> >>>>
> >>>>> What measures should be taken to avoid this sort of thing?
> >>>>
> >>>> WHY?
> >>>>
> >>>>
> >>>>> class Base
> >>>>> {
> >>>>> };
> >>>>>
> >>>>> class Derived1 : public Base
> >>>>> {
> >>>>> private:
> >>>>> int i, j, k;
> >>>>> };
> >>>
> >>>>> class Derived2 : public Base
> >>>>> {
> >>>>> private:
> >>>>> double l, m, n;
> >>>>> };
> >>>>>
> >>>>> void BaseAssign(Base& lhs, Base& rhs)
> >>>>> {
> >>>>> lhs = rhs;
> >>>>> }
> >>>>>
> >>>>> int main(int argc, char *argv[])
> >>>>> {
> >>>>> Derived1 d1;
> >>>>> Derived2 d2;
> >>>>>
> >>>>> BaseAssign(d1, d2);
> >>>>> }
> >>>>>
> >>>>> // End code snippet
> >>>>>
> >>>>> The only way I can see around preventing this sort of thing is
> >>>>
> >>>> Why prevent this sort of thing? Do you experience any problem?
> >>>> Please elaborate.
> >>>>
> >>>>
> >>>>> declaring the assignment operators and copy constructors of
> >>>>> non-leaf classes protected, and providing clone/create methods
> >>>>> instead of letting the operators/constructors be used.
> >>>>>
> >>>>> Do people generally just not worry about this sort of thing? I'm
> >>>>> absolutely paranoid about stuff like this.
> >>>>
> >>>> Well, could you share? Maybe I need to become paranoid about it as
> >>>> well...
> >>>>
> >>>> V
> >>>
> >>>
> >>> The classes must be getting sliced... I mean, how can a Derived2 be
> >>> assigned to a Derived1?
> >>>
> >>
> >> Where did you get that idea, about slicing? The Base subobject of
> >> 'd1' is simply made _the_same_ as the Base subobject of 'd2'. At
> >> least that's what it means [to me] *semantically*. Nothing more and
> >> nothing less.
> >>
> >> Whatever 'd1' has _above_and_beyond_ its 'Base' subobject, is kept
> >> intact. Whatever 'd2' has _above_and_beyond_ its 'Base' subobject,
> >> is not used at all in that operation. I suppose the following is
> >> even scarier to you:
> >>
> >> class Derived3 : public Derived2 {};
> >> class Derived4 : public Derived3 { std::string name; };
> >>
> >> ...
> >> Derived4 d4;
> >> BaseAssign(d1, d4);
> >>
> >> It shouldn't be. Relax. Take a deep breath. Nothing *bad* is
> >> happening here. If your problem domain prohibits that (for whatever
> >> reason, you did not say anything about the problem domain), you
> >> _could_ disable it by making 'Base' protected base class, but then
> >> LSP cannot be applied... Maybe that's what you want... Speak up,
> >> then.
> >>
> >> V[/color]
> >
> > You seem to get angry when people ask civil questions about things
> > they don't understand. I feel like I'm going to be shouted at just
> > for being inquisitive and trying to understand something properly.[/color]
>
> Oh, please...
>
> <shrug> Angry? I say "relax", and you think I am angry all of
> a sudden? What is it, inferiority complex? Did somebody use to
> shout at you for not understanding something? Never mind. I cannot
> help you fight your demons. I can only help you with C++. So,
> lighten up and ask your questions.[/color]
It's the way you type. You're very abrasive. I understand that you
don't mean to sound rude, but that's definitely the way you came across
here. And I don't have time to argue with you about further
inappropriate comments that you just made.
[color=blue][color=green]
> > So assignment doesn't work "polymorphically"?[/color]
>
> It might, if you declare the assignment operator 'virtual'. But your
> case doesn't have that. It definitely shouldn't do it by default, and
> it doesn't.[/color]
How would this work? If we were in BaseAssign and the /virtual/
assignment operator was called, how would it possibly assign from an
incompatible type? I'm sorry if I'm getting mixed up, I just need to
clear this up for my own sanity.
[color=blue]
>[color=green]
> > This differs to C#[/color]
>
> Well... If you learn C++ hoping that it behaves similarly to some
> other language that may have similar letters in its name, I strongly
> urge you not to. C++ is complex enough, but much more logical if you
> shed some preconceptions that have crept in when you were learning
> <insert_other_language_here>.[/color]
Fortunately I am not doing this, you're jumping to conclusions a bit. I
barely know C#, but I just about managed to write the equivalent to my
C++ test program to see what the results were. I only mentioned this as
an interesting comparison and an example of another way of thinking
about the problem.
[color=blue]
>[color=green]
> > (I
> > wrote a similar test, and found that the object of type Derived1
> > /becomes/ a Derived2 when assigned through a Base reference). I guess
> > this is why boxing works in C# with the universal base class "object".[/color]
>
> I don't know what "boxing" is. I never heard that term used WRT C++
> objects.
>
> C++ is a _statically_ typed language. Objects in C++ cannot change
> their nature because of a simple assignment of their bases. Type of
> every C++ object is defined at its creation and remains the property
> of the object until the object's destruction. I, for one, find it much
> more logical.[/color]
Yes. Interestingly enough, it is often said that C# is statically
typed, despite working in a different way in my test.
[color=blue]
> to
> Base classes are essentially members of their respective derived classes.
> Assigning new values to members doesn't change the nature of objects,
> does it? Only the state. Why should assigning new values base class
> subjects be any different?[/color]
This is why I wrote my test and asked these questions. Thank you.