小编Tra*_*rax的帖子

我应该使用手动分配来允许移动语义吗?

我有兴趣了解何时应该开始考虑使用移动语义来支持复制数据,具体取决于数据的大小和类的用法.例如,对于Matrix4类,我们有两个选择:

struct Matrix4{
    float* data;

    Matrix4(){ data = new float[16]; }
    Matrix4(Matrix4&& other){
        *this = std::move(other);
    }
    Matrix4& operator=(Matrix4&& other)
    {
       ... removed for brevity ...
    }
    ~Matrix4(){ delete [] data; }

    ... other operators and class methods ...
};

struct Matrix4{
    float data[16]; // let the compiler do the magic

    Matrix4(){}
    Matrix4(const Matrix4& other){
        std::copy(other.data, other.data+16, data);
    }
    Matrix4& operator=(const Matrix4& other)
    {
       std::copy(other.data, other.data+16, data);
    }

    ... other operators and class methods ...
};
Run Code Online (Sandbox Code Playgroud)

我相信有一些开销必须"手动"分配和释放内存,并且考虑到在使用此类时真正触及move构造的机会,内存大小如此之小的类的首选实现是什么?真的总是首选移动副本吗?

c++ c++11

6
推荐指数
1
解决办法
571
查看次数

IntelliJ快捷方式强制maven重新导入

IntelliJ IDEA是否有快捷方式强制maven项目重新导入(重新加载依赖项).我碰巧正在处理两个依赖项目,我在白天多次做这个动作.

提前致谢.

java intellij-idea maven

5
推荐指数
1
解决办法
5829
查看次数

标签 统计

c++ ×1

c++11 ×1

intellij-idea ×1

java ×1

maven ×1