【问题标题】:How to get rid of the ugly record如何摆脱丑陋的记录
【发布时间】:2012-05-10 17:08:55
【问题描述】:
class c {
    private:
        int n[10];
    public:
        c();
        ~c();
        int operator()(int i) { return n[i];};
};

class cc {
    private:

    public:
        c *mass;
        cc();
        ~cc();
        c& operator*() const {return *mass;};
};
int somfunc() {
    c *c1 = new c();

    cc * cc1 = new cc();

    (*cc1->mass)(1);



    delete c1;
}

我有一个指向 cc 类的指针,指向 c 类。

有没有办法摆脱这样的记录:

(*cc1->mass)(1);

然后写一些这样的想法:

cc1->mass(1);

不可能吗?

【问题讨论】:

  • 一开始为什么会有这么多指针?修复编码风格可能更有建设性,而不是担心为什么一段非常丑陋的代码看起来......真的很丑。

标签: c++ oop pointers syntax operator-overloading


【解决方案1】:

当我看到“c++”和“运算符重载”的标签时,我的心灵警报开启了。

C++ 运算符重载很复杂,一些运算符如“()”或“->”使其更加困难。

我建议,在重载运算符之前,创建一个具有相同目的的全局函数或方法,测试它是否有效,然后用运算符替换它。

全局好友函数示例:

class c {
    private:
        int n[10];      

    public:
        c();
        ~c();

        // int operator()(int i) { return n[i]; } 

        // there is a friend global function, that when receives a "c" object,
        // as a parameter, or declares a "c" object, as a local variable,
        // this function, will have access to the "public" members of "c" objects,
        // the "thisref" will be removed, when turned into a method
        friend int c_subscript(c thisref, int i) ;
};

int c_subscript(c* thisref, int i)
{
  return c->n[i];
}

int main()
{
  c* objC() = new c();
  // do something with "objcC"

  int x = c_subscript(objC, 3);
  // do something with "x"

  return 0;
} // int main(...)

局部函数(“方法”)示例:

class c {
    private:
        int n[10];      

    public:
        c();
        ~c();

        // int operator()(int i) { return n[i]; }

        int subscript(int i) ;
};

int c::subscript(int i)
{
  return this.n[i];
}

int main()
{
  c* objC() = new c();
  // do something with "objcC"

  int x = c->subscript(objC, 3);
  // do something with "x"

  return 0;
} // int main(...)

并且,最后使用重载运算符:

class c {
    private:
        int n[10];      

    public:
        c();
        ~c();   

        int subscript(int i) ;

        int operator()(int i) { return this.subscript(i); }
};

int c::subscript(int i)
{
  return this.n[i];
}

int main()
{
  c* objC() = new c();
  // do something with "objcC"

  int x = c->subscript(3);
  // do something with "x"

  int x = c(3);
  // do something with "x"

  return 0;
} // int main(...)

请注意,在最后一个示例中,我使用唯一标识符保留方法。

干杯。

【讨论】:

    【解决方案2】:

    总是可以这样做:

    class cc {
        private:
            c *_mass;
    
        public:
            c& mass() const {return *_mass;};
    };
    

    现在..

    cc1->mass()(1);
    

    【讨论】:

      【解决方案3】:

      如果mass 是一个对象,而不是一个指针,你可以使用你想要的语法:

      class cc {
        private:
      
        public:
          c mass;
          cc();
          ~cc();
          const c& operator*() const {return mass;};
      };
      
      …
      
          cc1->mass(1);
      

      【讨论】:

      • 我想用这样的东西:c* mass;
      • 如果将mass 设为指针,则必须先取消引用它,然后才能使用函数语法。如果将mass 设为对象,则可以直接使用函数语法。你必须决定你想要更多的功能。
      【解决方案4】:

      你可以用

      (*(*cc1))(1)
      

      因为operator() 应用于对象,而不是指针。

      【讨论】:

        【解决方案5】:

        你可以使用

        (**cc1)(1);
        

        或者

        cc1->mass->operator()(1);
        

        【讨论】:

          猜你喜欢
          • 2014-06-21
          • 2011-03-15
          • 1970-01-01
          • 1970-01-01
          • 2012-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多