Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

c++ - Preventing slicing in copy constructor

I want to copy a vector of type Foo objects but the objects can be several different derived types of Foo. I can't figure out how to copy without slicing. Here's my toy code

#include "stdafx.h"
#include <memory>
#include <vector>
#include <string>
#include <iostream>

class Foo
{
public:
    Foo() { m_x = "abc"; }
    Foo( const Foo &other ) { m_x = other.m_x; }
    virtual std::string ToString() { return m_x; }
    std::string m_x;
};

class FooDerivedA : public Foo
{
public:
    FooDerivedA() : Foo() { m_y = 123; }
    std::string ToString() { return m_x + ", " + std::to_string( m_y ); }
    int m_y;
};

class FooDerivedB : public Foo
{
public:
    FooDerivedB() : Foo() { m_z = true; }
    std::string ToString() { return m_x + ", " + std::to_string( m_z ); }
    bool m_z;
};

class Foos
{
public:
    Foos(){}
    Foos( const Foos &other )
    {
        for ( auto &foo : other.m_Foos )
        {
            // I believe this is slicing. How can I prevent this?
            auto f = std::unique_ptr<Foo>( new Foo( *foo ) ); 
            m_Foos.push_back( std::move( f ) );
        }
    }
    void Add( std::unique_ptr<Foo> foo ) { m_Foos.push_back( std::move( foo ) ); }
    std::string ToString() 
    {
        std::string s;
        for ( auto &foo : m_Foos )
        {
            s += foo->ToString() + "
";
        }
        return s;
    }
private:
    std::vector<std::unique_ptr<Foo>> m_Foos;
};

int main()
{
    Foos f1;
    f1.Add( std::unique_ptr<FooDerivedA>( new FooDerivedA ) );
    auto f2 = Foos( f1 );
    std::cout << "f1:" << f1.ToString() << std::endl;
    std::cout << "f2:" << f2.ToString() << std::endl;
    system("pause");
    return 0;
}

I can't specify that the type should be FooDerivedA like:

auto f = std::unique_ptr<Foo>( new FooDerivedA( *foo ) ); 

because it might be FooDerivedB. How can I copy the data without slicing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...