您必须定义move ctor
and move assignment operator
,Image
class
因为默认实现将被删除,因为您提供了用户声明的copy ctor
and copy assignment operator
。
然后,您应该可以毫无问题地使用vector
。
class Image
{
public:
Image( int height, int width )
: height_{ height }
, width_{ width }
{ }
Image( Image&& ) = default;
Image& operator=( Image&& ) = default;
Image( const Image& ) = delete;
Image& operator=( const Image& ) = delete;
private:
int height_;
int width_;
};
class ImageInformation
{
public:
explicit ImageInformation( Image image )
: image_{ std::move( image ) }
{ }
private:
Image image_;
};
int main( )
{
std::vector<ImageInformation> containers;
Image image{ 10, 10 };
containers.emplace_back( std::move( image ) );
}