Win32 C++从类返回 cout 功能

Win32 C++ Return cout Functionality From a Class

本文关键字:cout 功能 返回 C++ Win32      更新时间:2023-10-16

>我正在处理一个简单的类,因此如果我使用 cout "equivalent",它将显示控制台,如果我不这样做,控制台不会弹出。

最终结果,如果可能的话,尝试使用一些简单的东西,例如:O.C() << "any type of data";O.C("any type of data");相当于std::cout流。这次支线旅行是为了让我知道它是否被使用。

// the obvious, shows and opens a/the console window so I can use the cout and cin streams.
void ShowConsole() {
  AllocConsole();
  HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
  int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
  FILE* hf_out = _fdopen(hCrt, "w");
  setvbuf(hf_out, NULL, _IONBF, 1);
  *stdout = *hf_out;
  HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
  hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
  FILE* hf_in = _fdopen(hCrt, "r");
  setvbuf(hf_in, NULL, _IONBF, 128);
  *stdin = *hf_in;
}

从那里,我不知道该怎么办。为了简单起见,我正在编写的类全局定义为"O"。函数C()是我尝试用来使用流的函数。

我不知道在这里该怎么办,要么想办法用C()返回句柄,要么想办法让C()接受任何类型的数据。

该类是基本的,看起来像:

class testout {
private: 
    bool display;
public:
    void ShowConsole();
    void C(); // which could return a handle, or accept any data type
}

任何帮助都值得赞赏,我也可能在地方使用了一些不正确的术语。

我想

我不妨手动输入类型。

void testout::TOUT(int i) {cout << i;}
void testout::TOUT(double d) {cout << d;}
void testout::TOUT(char c) {cout << c;}
void testout::TOUT(char* cstr) {cout << cstr; }
void testout::TOUT(string str) {cout << str; }

在类中,对于这些函数,我将另外附加显示的 true 更新。