C++ 声明另一个类似于cout的对象输出流

C++ 声明另一个类似于cout的对象输出流,c++,cout,C++,Cout,我只是好奇,是否有可能以某种方式声明类似于cout的东西,但定义它的功能。例如: SetCursorPosition(output_handle, some_COORD); cout_coord << "Hi\nthere!"; cout不是一个函数,它实际上是一个std::ostream。您可以创建自己的ostream来写入标准输出 可以使用运算符重载使实现此逻辑的方法是创建一个自定义流缓冲区,该缓冲区在其overflow()方法的适当位置写入,然后使用此流缓冲区创建合适的std

我只是好奇,是否有可能以某种方式声明类似于cout的东西,但定义它的功能。例如:

SetCursorPosition(output_handle, some_COORD);
cout_coord << "Hi\nthere!";

cout不是一个函数,它实际上是一个std::ostream。您可以创建自己的ostream来写入标准输出

可以使用运算符重载使
实现此逻辑的方法是创建一个自定义流缓冲区,该缓冲区在其
overflow()
方法的适当位置写入,然后使用此流缓冲区创建合适的
std::ostream
。下面是一个大概的草图,它看起来是什么样子:

class posbuf: public std::streambuf {
    int d_row;     // current row
    int d_column;  // column where to start output
    void move() { /* use curses, VT100 codes, whatever to move the cursor */ }
    int overflow(int c) {
        if (c == traits_type::eof()) { return traits_type::not_eof(c); }
        if (c == '\n') { ++this->d_row; this->move(); return '\n'; }
        return std::cout << traits_type::to_char_type(c)? c: traits_type::eof();
    }
    int sync() {
        return std::cout.flush()? 0: -1;
    }
public:
    posbuf(int row, int column): d_row(row), d_column(column) { this->move(); }
};
struct oposstream: private virtual posbuf, public std::ostream {
    oposstream(int row, int column): posbuf(row,c column), std::ostream(this) {}
};

int main() {
    oposstream out(4, 10);
    out << "hello\nworld";
}
类posbuf:public std::streambuf{ int d_row;//当前行 int d_column;//开始输出的列 void move() 整数溢出(intc){ if(c==traits_-type::eof()){return traits_-type::not_-eof(c);} 如果(c=='\n'){++this->d_行;this->move();返回'\n';} 返回std::cout move();} }; struct oposstream:private virtual posbuf,public std::ostream{ oposstream(int行,int列):posbuf(行,c列),std::ostream(this){} }; int main(){ oposstreamout(4,10);
由于Christian Hackl和另一位用户删除了他们的答案,我做了一项研究,找到了我想要的答案

如果有人仍然好奇,我在问题中的例子是:

std::ostream& operator<<(std::ostream& left, const char * right)
{
    string sRight = right;
    CONSOLE_SCREEN_BUFFER_INFO con;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &con);
    COORD xy;
    xy.X = con.dwCursorPosition.X;
    xy.Y = con.dwCursorPosition.Y;
    for (int i = 0; i < sRight.length(); i++)
    {
        if (sRight[i] == '\n')
        {
            xy.Y++;
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);                
            continue;
        }
        cout << sRight[i];
    }
    return left;
}

<代码> STD::C++中的OsFase+Calpor,大家都知道<>代码>实现一个不与操作员混在一起的更好的方法是创建一个 Orths包装器,它格式化输出并公开允许设置格式化选项的成员函数,或者使用自定义的操作器。只是使用现有的操作器,比如代码>宽度()/<代码>和对齐的。这很有趣,但是看起来有点复杂,我还没有完成C++的书,我对你使用的一些功能不太熟悉。我会查查看,但实际上我找到了我正在看的,看到我在这个问题上的答案。
std::ostream& operator<<(std::ostream& left, const char * right)
{
    string sRight = right;
    CONSOLE_SCREEN_BUFFER_INFO con;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &con);
    COORD xy;
    xy.X = con.dwCursorPosition.X;
    xy.Y = con.dwCursorPosition.Y;
    for (int i = 0; i < sRight.length(); i++)
    {
        if (sRight[i] == '\n')
        {
            xy.Y++;
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);                
            continue;
        }
        cout << sRight[i];
    }
    return left;
}
SetConsoleCursorPosition(output_handle, some_COORD);
cout_coord << "Hi\nthere!";