代码之家  ›  专栏  ›  技术社区  ›  Emile Cormier

What to do with proxy class copy-assignment operator?

  •  1
  • Emile Cormier  · 技术社区  · 14 年前

    class VertexProxy
    {
    public:
        VertexProxy(double* x, double* y, double* z)
        : x_(x), y_(y), z_(z) {}
    
        VertexProxy(const VertexProxy& rhs)
        : x_(rhs.x_), y_(rhs.y_), z_(rhs.z_) {}
    
        // Coordinate getters
        double x() const {return *x_;}
        double y() const {return *y_;}
        double z() const {return *z_;}
    
        // Coordinate setters
        VertexProxy& x(double val) {*x_ = val; return *this;}
        VertexProxy& y(double val) {*y_ = val; return *this;}
        VertexProxy& z(double val) {*z_ = val; return *this;}
    
        VertexProxy& operator=(const VertexProxy& rhs)
        {
            // Should it be this
            x_ = rhs.x_; y_ = rhs.y_; z_ = rhs.z_;
    
            // or this?
            *x_ = *rhs.x_; *y_ = *rhs.y_; *z_ = *rhs.z_;
    
            return *this;
        }
    
    private:
        double* x_; double* y_; double* z_;
    };
    

    我需要能够重置代理,以便它持有不同的坐标指针(类似于 boost::shared_ptr.reset() . 此外,我希望能够将坐标值分配给来自不同代理(即 proxy1.assign(proxy2) )

    What should be the meaning of operator= in my class above? 抄袭 rhs 指针(浅拷贝)或 RHS 价值观?还是我应该做 运算符= 私有并提供两个成员函数以避免 运算符= ?

    编辑:

    好的,这是一些背景资料。我正在围绕第三方GIS库(shapelib)编写一个包装器,该库将顶点坐标(x、y、z、m)存储在单独的数组中(而不是结构数组)。我的代理类用于使数组的这个结构看起来更像一个结构数组。它与一个自定义顶点迭代器类协同工作,使其更容易处理顶点范围。

    shapelib处理内存管理。我的代理类所做的就是在顶点数据中显示一个不同的“视图”。当用户使用我的代理操作顶点坐标时,它实际上操作shapelib形状对象中的顶点坐标。

    4 回复  |  直到 14 年前
        1
  •  2
  •   Puppy    14 年前

    很简单。是否希望VertexProxy充当指针或值?如果您希望它像指针一样工作,那么复制指针;如果您希望它像值一样工作,那么复制值。没有人能告诉你你的类是一个指针或一个值(特别是你似乎有一些不寻常的东西)。如果你想得到更好的建议,我们需要知道什么是真正的双打,为什么。

    快速编辑: 在我看来,实际上,如果你做了解引用,你会让它像一个引用或指针。但是,原始点保持不变。

        2
  •  3
  •   CB Bailey    14 年前

    假设您的复制构造函数复制指针,为了一致性,您的复制分配操作符应该分配指针。

    VertexProxy& operator=(const VertexProxy& rhs)
    {
        x_ = rhs.x_;
        y_ = rhs.y_;
        z_ = rhs.z_;
    
        return *this;
    }
    

    VertexProxy test( const VertexProxy& other )
    {
        double tmp1, tmp2, tmp3;
        VertexProxy p1( &tmp1, &tmp2, &tmp3 );
        p1 = other;
        return p1;
    }
    

    acted differently to:

    VertexProxy test( const VertexProxy& other )
    {
        double tmp1, tmp2, tmp3; // unused
        VertexProxy p1( other );
        return p1;
    }
    
        3
  •  1
  •   Emile Cormier    14 年前

    std::bitset::reference performs a role similar to my VertexProxy and can be used as a model.

    typedef std::bitset<8> Bitset;
    Bitset bset1, bset2;
    Bitset::reference r1(bset1[3]);
    Bitset::reference r2(bset2[3]);
    r1 = 1;
    r2 = r1;
    std::cout << "bset2 = " << bset2 << "\n";
    

    r2 = r1 above copies values.

        4
  •  0
  •   Zan Lynx    14 年前

    I would say that it depends on how large the object is.

    如果代理主题非常大,那么可以使用引用计数共享指针。只需在复制操作中复制共享指针即可。

    If it isn't that big then a deep copy is better. Less hassle all around for everyone.