By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

own function with cout, how to concatenate argument ?

P: n/a
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.

Sep 21 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"vf***@talktalk.net" <vf***@talktalk.netwrites:
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
Sep 21 '06 #2

P: n/a
vf***@talktalk.net wrote:
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::ostream &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.

Sep 21 '06 #3

P: n/a
vf***@talktalk.net wrote:
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::ostream &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.

Sep 21 '06 #4

P: n/a
>
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 ?

Sep 22 '06 #5

P: n/a
vf***@talktalk.net wrote:
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 ?
Something like this:

struct LogHelper
{
LogHelper(std::ostream &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::ostream &operator<<(T const &t)
{
return os << t;
}

operator std::ostream &() { return os; }

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

private:
std::ostream &os;
bool active;
};

Sep 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.