我试图通过重载流来在c ++中同时写入文件和标准输出
test.h
#pragma once
#include <iostream>
using std::ofstream;
class OutputAndConsole:public ofstream
{
public:
std::string fileName;
OutputAndConsole(const std::string& fileName):ofstream(fileName),fileName(fileName){
};
template <typename T>
OutputAndConsole& operator<<(T var);
};
template <typename T>
OutputAndConsole& OutputAndConsole::operator<<(T var)
{
std::cout << var;
ofstream::operator << (var);
return (*this);
};
Run Code Online (Sandbox Code Playgroud)
TEST.CPP
OutputAndConsole file("output.txt");
file << "test" ;
Run Code Online (Sandbox Code Playgroud)
文件中的输出是
01400930
Run Code Online (Sandbox Code Playgroud)
但在控制台是
test
Run Code Online (Sandbox Code Playgroud)
我调试了它看起来正在进入的代码
_Myt& __CLR_OR_THIS_CALL operator<<(const void *_Val)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?