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

时间:2019-08-27 23:25:04

标签: c++ ncurses

我想要一种允许将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

0 个答案:

没有答案
相关问题