S
sven.suursoho
Hello,
In main(), the first output API is what I try to achieve. Unfortunately
it fails, printing first string as pointer instead of human readable
message. Tried to initialize str(""), set new buffer etc, but nothing
worked.
Ideas? Also might use another internal construct, only API is needed
and requirement to reuse existing std:
stream inserters.
App itself:
#include <iostream>
#include <sstream>
class printer
{
public:
void print (const std::string &s) {
std::cout << s << std::endl;
}
};
class stream: public std:
stringstream
{
public:
stream (printer &p): std:
stringstream(), printer_(p) {
// gets stored correctly
*this << "*** ";
}
~stream () {
printer_.print(str());
}
std:
stream &
operator* () {
return *this;
}
private:
printer &printer_;
};
int
main ()
{
printer p;
// want to achieve this but:
// prints address of string instead of "first"
stream(p) << "first" << " fails";
// works because first inserted object is not printed
stream(p) << std::flush << "ok 1";
// works because stream object is used through 'operator*'
// can be any other method returning this or *this
*stream(p) << "ok 2";
return 0;
}
In main(), the first output API is what I try to achieve. Unfortunately
it fails, printing first string as pointer instead of human readable
message. Tried to initialize str(""), set new buffer etc, but nothing
worked.
Ideas? Also might use another internal construct, only API is needed
and requirement to reuse existing std:
App itself:
#include <iostream>
#include <sstream>
class printer
{
public:
void print (const std::string &s) {
std::cout << s << std::endl;
}
};
class stream: public std:
{
public:
stream (printer &p): std:
// gets stored correctly
*this << "*** ";
}
~stream () {
printer_.print(str());
}
std:
operator* () {
return *this;
}
private:
printer &printer_;
};
int
main ()
{
printer p;
// want to achieve this but:
// prints address of string instead of "first"
stream(p) << "first" << " fails";
// works because first inserted object is not printed
stream(p) << std::flush << "ok 1";
// works because stream object is used through 'operator*'
// can be any other method returning this or *this
*stream(p) << "ok 2";
return 0;
}