gpt4 book ai didi

c++ - 在使用 cout 时如何创建一个操作文本的类?

转载 作者:太空宇宙 更新时间:2023-11-04 13:30:18 25 4
gpt4 key购买 nike

我想创建一个在每个字符之间延迟的操纵器,就像我写的一样

delay wait = 40;
cout << wait << "Hello World!";

它应该输出“H”然后是 Sleep(40)、“e”然后是 Sleep(40)、“l”然后是 Sleep(40) 等等,我试图为此编写一个类,这是我的代码:

class delay {

public:
delay(const int& amount) { // getting the amount for Sleep(...)
this->Amount = amount;
}
delay& delay::operator = (const int& amount) { // same, but for operator=
this->Amount = amount
return *this;
}

private:
ostream& m_os;
int Amount;
friend ostream& operator << (ostream& os, delay& p) { // i dont know if I even need this
p.m_os = os;
return os;
}
friend ostream& operator << (delay& p, const char* n) { // here it should do output and delay
int index = 0;
while (n[index] != '\0) {
p.m_os << n[index]; // output character
index++;
Sleep(p.Amount); // wait
}
return p.m_os;
}

};

当我尝试使用这段代码时出现错误,我觉得我必须从头开始重写它。我希望你知道如何实现这个延迟类,因为我已经尽我所能但它不起作用。感谢您阅读本文,希望您能帮助我<3

最佳答案

cout << wait << "Hello World!";(cout << wait) << "Hello World!";所以你的运营商永远不会被调用。简单的调试就会显示这一点!基本上,您将需要一个代理对象。不过,我认为您不应该这样做。你会阻塞线程。为什么不使用适当的任务调度程序按计划生成输出?

关于c++ - 在使用 cout 时如何创建一个操作文本的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31752841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com