own function with cout, how to concatenate argument ?

V

vfunc

if I want to output some text in my own function how can I concatenate
with the argument ? Example

void mycout(const char *c)
{
std::cout << c << std::endl;
}

mycout("Here is some text. " << " And here is some more");

Which will not compile.
 
J

Jens Theisen

if I want to output some text in my own function how can I concatenate
with the argument ? Example

void mycout(const char *c)
{
std::cout << c << std::endl;
}

mycout("Here is some text. " << " And here is some more");

Which will not compile.

Use std::string in favor of const char *. For example,

void mycout(std::string const& c)
{
std::cout << c << std::endl;
}

int main()
{
std::string str("Here is some text. ");
str.append(" And here is some more");

mycout(str);
}

or

int main()
{
std::stringstream s;

s << "Here is some text. " << " And here is some more";

mycount(s.str());
}

Regards,

Jens
 
O

Old Wolf

if I want to output some text in my own function how can I concatenate
with the argument ? Example

void mycout(const char *c)
{
std::cout << c << std::endl;
}

mycout("Here is some text. " << " And here is some more");

std::eek:stream &mycout() { return std::cout; }
mycout() << "Here is some text. "
<< " And here is some more" << std::endl;

If you want an automatic endl at the end of the calling full
expression, you need to make mycout() return a temporary
object that implements operator<< and does stuff in its destructor.
 
O

Old Wolf

if I want to output some text in my own function how can I concatenate
with the argument ? Example

void mycout(const char *c)
{
std::cout << c << std::endl;
}

mycout("Here is some text. " << " And here is some more");

std::eek:stream &mycout() { return std::cout; }
mycout() << "Here is some text. "
<< " And here is some more" << std::endl;

If you want an automatic endl at the end of the calling full
expression, you need to make mycout() return a temporary
object that implements operator<< and does stuff in its destructor.
 
V

vfunc

If you want an automatic endl at the end of the calling full
expression, you need to make mycout() return a temporary
object that implements operator<< and does stuff in its destructor.

How do you implement that operator ?
Care to expand ?
 
O

Old Wolf

How do you implement that operator ?
Care to expand ?

Something like this:

struct LogHelper
{
LogHelper(std::eek:stream &os_): os(os_), active(true)
{
std::time_t t = std::time(NULL);
char buf[100];
std::strftime(buf, sizeof buf, "%d-%b-%Y %H:%M:%S",
std::localtime(&t));
os << "[" << buf << "] ";
}

LogHelper(LogHelper &h): os(h.os), active(true)
{
h.active = false;
}

template<typename T>
std::eek:stream &operator<<(T const &t)
{
return os << t;
}

operator std::eek:stream &() { return os; }

~LogHelper()
{
if (active)
os << std::endl;
}

private:
std::eek:stream &os;
bool active;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top