【问题标题】:How to call the overloaded version of operator() [closed]如何调用 operator() 的重载版本 [关闭]
【发布时间】:2021-07-07 07:50:50
【问题描述】:
#include <iostream>

struct S
{
    void operator()(char c){ std::cout << c << std::endl; };
    void operator()(const char * p)
    {
        while( *p != '\0' )
        {
             this( *p++ );  # error: expression cannot be used as a function
        }
    }
} s ;

int main()
{
    s( "blqhblqh" );
}

如何调用重载版本?

【问题讨论】:

  • (*this)(*p++)this-&gt;operator()(*p++)operator()(*p++)decltype(this)struct S* 不是 struct S

标签: c++ operator-overloading


【解决方案1】:

operator() 应该在S 的实例上调用,但不是像this 那样指向S 的指针。所以

(*this)( *p++ );

或者更难看直接调用operator()

this->operator()( *p++ );
(*this).operator()( *p++ );

【讨论】:

  • 天啊,真是个愚蠢的错误。感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
相关资源
最近更新 更多