【问题标题】:how we use operators of implementation class with pointers我们如何使用带有指针的实现类的运算符
【发布时间】:2015-02-07 00:45:16
【问题描述】:

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 idiom 上使用它们是真的吗?

【问题讨论】:

  • 有一个模式:operator &lt;symbol&gt;&lt;symbol&gt; operator 与该模式不匹配。
  • 编译器会准确地告诉您代码出了什么问题。您是否尝试阅读错误消息?
  • 错误:没有匹配函数调用 'VecMD_Impl::operator+=(const VecMD&)' @Code::Blocks
  • 我知道错误的含义,谢谢。 需要阅读并理解它。

标签: c++ design-patterns operators implementation


【解决方案1】:

解决的问题
VecMD& operator += (const VecMD& Vec)
{
     pimple -> operator +=(*Vec.pimple);
     return *this;
}

实现类相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2021-09-08
    相关资源
    最近更新 更多