Preventing slicing in copy constructor

亡梦爱人 提交于 2019-11-29 14:35:55

The classical method to solve this problem is to implement a virtual Foo *clone() const, which is then called instead of the copy constructor.

So, if we have an object of some (derived form of) Foo in x, we can create another one by:

 void someFunc(Foo *x)
 {
     Foo *copy_of_x = x->clone();
     ... 
     delete copy_of_x;  // Important so we don't leak!
 }

Note that since it's a virtual function, we can't call it in the constructor of foo or any of it's derived types, as virtual functions don't operate "correctly" inside constructors.

You may consider using Boost.Variant instead of pointers in your container. This avoid a lot of slicing and memory management issues. Also, you get a lot more from default constructors.

Here is a full rework of your example using this design:

#include <vector>
#include <iterator>
#include <string>
#include <boost/variant.hpp>

struct Foo
{
    Foo() : m_x("abc") {}
    std::string m_x;
};

struct FooDerivedA : Foo
{
    FooDerivedA() : m_y(123) {}
    int m_y;
};

struct FooDerivedB : Foo
{
    FooDerivedB() : m_z(true) {}
    bool m_z;
};

typedef boost::variant<FooDerivedA, FooDerivedB> a_foo;

struct to_string : boost::static_visitor<std::string>
{
    std::string operator()(Foo const& foo) const 
        {return foo.m_x;}
    std::string operator()(FooDerivedA const& foo) const
        {return foo.m_x + ", " + std::to_string(foo.m_y);}
    std::string operator()(FooDerivedB const& foo) const
        {return foo.m_x + ", " + std::to_string(foo.m_z);}
};

std::ostream& operator<<(std::ostream& os, a_foo const& foo)
{
    return os << boost::apply_visitor(to_string(), foo);
}

int main()
{
    std::vector<a_foo> f1;
    f1.push_back(FooDerivedA());
    f1.push_back(FooDerivedB());
    auto f2 = f1;
    std::ostream_iterator<a_foo> out_it(std::cout, "\n");
    std::cout << "f1:" << std::endl;
    std::copy(f1.begin(), f1.end(), out_it);
    std::cout << "f2:" << std::endl;
    std::copy(f2.begin(), f2.end(), out_it);
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!