473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

own function with cout, how to concatenate argument ?

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
5 4737
"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
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
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
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double...
5
by: Andrej Prsa | last post by:
Hi! Why do I get a warning about incompatible pointer type if I try to assign a pointer to the function with variable argument number: int func (int argc, ...) , but everything is ok...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
4
by: Damien | last post by:
Hi all, I've run into something confusing on MS VC6. Yeah I know it's old but that's what the client wants, so... I'm trying to pass a pointer to a member function as a template argument,...
2
by: Hexman | last post by:
Hello All, In SS EE I have nulls in a column. I want to select and eventually change to a zero (its a smallint column). I've tried selecting 'null', 'dbnull', etc. Then I read about the ISNULL...
2
by: Alec.Edgington | last post by:
Hi, Quick question: I'm wondering if there is, Somewhere Out There, a Python implementation of the Riemann zeta function for complex argument...? Thanks for any help. A.
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
7
by: ndac | last post by:
Hello, How should I pass a function as an argument? Regards, Pieter
1
by: Bharath25 | last post by:
Getting the following error while using the update query below: Msg 174, Level 15, State 1, Procedure PY_RULE_EABOS2010, Line 165 The isnull function requires 2 argument(s). update tmp set...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.