我们如何使用指针实现类的运算符

问题描述 投票:-3回答:1

Vec_MD类是用VecMD_Impl实现的。使用pimple指针无法从实现类实现运算符

实现的类是:

class VecMD{
private:
      VecMD_Impl *pimple;
public: 
     ///other elements..//
     ///operators//
     double& operator [] (int index){  //works
           return pimple -> operator[](index);
     }
     VecMD& operator +=(const VecMD& Vec){  //errors
           return pimple -> operator += (Vec);
     }};

实现类是

class VecMD_Impl{
private:
     double *p_;
     int dim;
public:
     ////other elements....////
     //opearators//
     double& operator[](int index){
          return p_[index];
     }
     VecMD_Impl& operator +=(const VecMD_Impl& Vec){
          for(int i = 0 ; i <= Vec.dim; ++i){
               p_[i] += Vec.p_[i];
          }
          return *this;
     }};

是真的 - >在实现类上使用运算符?

如果我们如何在pimpl成语中使用它们是真的吗?

c++ design-patterns operators implementation
1个回答
0
投票

问题已解决

VecMD& operator += (const VecMD& Vec)
{
     pimple -> operator +=(*Vec.pimple);
     return *this;
}

实现类是一样的。

© www.soinside.com 2019 - 2024. All rights reserved.