繁体   English   中英

类型转换`operator`函数返回指向函数的指针

[英]Type conversion `operator` function returning pointer to function

我想要一种允许terminfo转义序列(特别是由于调用tparm()而导致的结果)存储在字符串中的方法,因此我编写了这个包装器类。

class tputs_wrapper {
    private:
        static string *current;

    public:
        using putchar_like=int (*)(int);

        tputs_wrapper(string &s){
            s.clear();
            current=&s;
        }

        static int putchar(int ch){
            *current+=static_cast<unsigned char>(ch);
            return ch;
        }

        operator putchar_like(){ return putchar; }
};
string *tputs_wrapper::current=nullptr;

可以如下使用(粗略示例)。

tputs(tparm(set_a_foreground, 196), 1, tputs_wrapper(sp));  // Invokes the type conversion operator for tupts' thrid arg.
tputs(tparm(exit_attribute_mode), 1, tputs_wrapper(ep));    // Ditto.
cout << sp << "Pink." << ep << "\nNormal\n";

问题:有没有一种声明运算符的方法,该运算符允许将该类的对象解释为指向采用int并返回int的函数的指针,而无需诉诸类型别名putchar_like

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM