Re: new Class(*this)
On Thursday, 9 April 2015 02:41:11 UTC+3, Christopher Pisz wrote:
On 4/8/2015 6:25 PM, =D6=F6 Tiib wrote:
SNIP
It is not silly. Sometimes we need dynamic polymorphism in C++.
Often we need it for components of bigger object to be dynamically
polymorphic. Sometimes we later need a deep copy made from object that
contains dynamically polymorphic components. Lack of such 'virtual clon=
e'
will cause that deep copy to be a big silly mess.
Can you give an example of the problem?
Slicing will typically cause damaged invariant of most derived object.
struct A
{
A(int a) : a_var(a) {}
int a_var;
};
struct B : public A
{
B(int a, int b) : A(a), b_var(b) {}
int b_var;
};
B &getB()
{
static B b(1, 2);
return b;
}
int main()
{
// Assignment by value to a
A a(3);
a = getB();
// base class subobject of b is copied to a
// that may be is not what we want
B b2(3, 4);
A &a2 = b2;
a2 = getB();
// only base class subobject was assigned so
// b2.a_var == 1, b2.b_var == 4!
// that is next to never what we want
return 0;
}
I am sure there is a better way
then defining a Clone method that returns a raw pointer.
No. There are covariance of raw pointers but there are no
covariance of smart pointers. So returning smart pointer
would lose virtual of clone method.
If I've got a Fish and I want to make a copy that is a Tuna, I can't and=
shouldn't be able to, until I know the Fish is a Tuna through dynamic
cast and check.
With clone you can have pool of different fishes and still make
deep copy of it. No typeid checking or dynamic casting is needed.
The typeid checking and dynamic casting involves that designer of
pool of fishes has to have deep knowledge about all of fish types
that can be in it. It is ugly to read and error-prone if new
fish class is added. That is silly mess of design.
If I've got a Tuna and I want a Fish, I can copy construct another Tuna=
and access everything that is part of the Fish.
The whole point of dynamic polymorphism is that you can have
a Fish (std::unique_ptr<Fish>) that may be only run-time known
if it is Tuna or Shark. It did enter the "gulf" from "sea"
and so only that 'unique_ptr' value was passed over.
Maybe I am not getting what you are driving at with the description of
"dynamically polymorphic". Google doesn't seem to know about the phrase=
either, just its individual components, which I am sure we all understand=
..
Class is dynamically polymorphic when it contains virtual functions.
Often we do not need any virtual functions but sometimes we really
do. It is typically case when we will have otherwise repetitive
table lookup, if-else and/or switch-case chain over some sort of
"kind" or "type" member value in C to differentiate between behavior.
C++ has virtual functions to make it bit less of a mess and it also
can perform better.
In some languages like Objective-C or Javascript all methods are
virtual. That is at other edge evil since it makes the things too
loose and it also hurts performance a bit when virtual is not needed.