在这种情况下有什么方法可以使用 function_Pointer 吗?

时间:2021-03-26 07:14:59

标签: c++

我很难知道 ios::hex 函数是如何工作的。

#include <iostream>
#include <string>   
#include <ios>
#include <ostream>
#include <istream>


class A {
public:
 A& operator >> (std::ios_base& (*pf)(std::ios_base&)) {
     (*pf);
     return *this;
 }
 A& operator >> (int& a) {
     std::cin >> a;
     return *this;
 }
};

int main() {
 int b;
 A a;
 a >> std::hex >> b;
 std::cout << "입력 : " << b << std::endl;

 return 0;
}

重点是,(*pf) 不是执行 'ios::hex 函数'的方法

std::ios_base& hex(std::ios_base& str); : str -> 我不知道如何给这个参数,比如“ pf(??) ”

所以我决定转换更简单的方法并弄清楚如何工作。

namespace yy {
    int sum(int a, int b) {
        return a + b;
    }
}
class A {
public:
    A& operator >> (int (*pf)(int,int)) {
        pf;     // ---> 
        return *this;
    }
    A& operator >> (int& a) {
        std::cin >> a;
        return *this;
    }
};


int main() {
    int b;
    A a;
    a >> yy::sum >> b;   // or a >> yy::sum(4,5) >> b ;  ???
    std::cout << "입력 : " << b << std::endl;

    return 0;
}

但是,我仍然不知道如何使用它..

请帮我

1 个答案:

答案 0 :(得分:0)

std::hex 用于设置 std::ios_base::flags。您的类 A 不是派生自 std::ios_base,也没有派生自 std::ios_base 的成员。所以确实没有明显的地方来存储 std::ios_base::hex 标志。

代码表明您想要影响 std::cin::flags。这有点奇怪,因为 cin 并不真正属于特定的 A 对象。但让我们假设这就是你想要的。然后你可以写std::cin >> pf

相关问题