我想创建一个在每个字符之间延迟的操纵器,就像我写的那样
delay wait = 40;
cout << wait << "Hello World!";
它应该输出'H'然后睡眠(40),'e'然后睡眠(40),'l'然后睡眠(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
答案 0 :(得分:0)
cout << wait << "Hello World!";
is (cout << wait) << "Hello World!";
so your operator never gets invoked. Simple debugging would have shown this! You will need a proxy object, basically. Still, I don't think you should do this. You'll block the thread. Why not use a proper task scheduler to produce output on a schedule?