Hi,
folgendes Problem: Ich habe eine Klasse von streambuf abgeleitet, um
cout (umzubiegen). Sehe nachfolgenden Code.
1 | class NRedirectStream : public streambuf {
|
2 | HWND control;
|
3 | public:
|
4 | //typedef std::char_traits<char> traits_type;
|
5 | //typedef traits_type::int_type int_type;
|
6 |
|
7 | NRedirectStream(HWND hwnd) {
|
8 | control = hwnd;
|
9 | };
|
10 | virtual streamsize xsputn(const char_type *_Ptr, streamsize _Count) {
|
11 | unsigned long length = GetWindowTextLength(control);
|
12 | wchar_t *text = new wchar_t[length+_Count*2+1];
|
13 | GetWindowText(control, text, length+1);
|
14 | wchar_t* target = text+length;
|
15 | const char* source = _Ptr;
|
16 | for (int i = 0; i<_Count; i++) {
|
17 | char c = *source++;
|
18 | if (c == '\n') *target++ = '\r';
|
19 | *target++ = (wchar_t)c;
|
20 | }
|
21 | *target = 0;
|
22 | SetWindowText(control, text);
|
23 | delete [] text;
|
24 | return _Count;
|
25 | return __super::xsputn(_Ptr, _Count);
|
26 | };
|
27 |
|
28 | virtual int_type overflow(int_type c) {
|
29 | return traits_type::not_eof(c);
|
30 | };
|
31 | };
|
Das ganze funktionniert auch teilweise.
Beispiel:
cout << "Der Name ist " << name << ".\n";
Von dieser Zeile wird für "Der Name ist " und ".\n" jeweils xsputn
aufgerufen. Nur für die Variable name (vom Type string in C++) nicht.
Weiß jemand, woran es liegen kann, dass Literale richtig verarbeitet
werden und Strings (c++ Klasse string) als Variable keinen Aufruf von
xsputn erzeugen? Auch wenn ich int, long, float übergebe, streikt meine
Klasse.