Feb 7, 2012 at 10:53pm UTC
hi just wondering if someone could give me an example on how to pass a std sprintf function into a parameter of a function thanks.
So the function I want it able to do is this.
_log(sprtinf(buffer," %s some error %s",time, done),para1,para2);
just wondering if this is possible or even this will do.
_log(prtinf(" %s some error %s",time, done),para1,para2);
thank-you
Feb 7, 2012 at 11:03pm UTC
1 2 3 4 5
void MyFunc(int MyParam, int (*PrintFunc)(char *, const char *, ...) = 0, char *buffer = 0)
{
//code
if (PrintFunc && buffer) PrintFunc(buffer, "%d" , MyParam);
}
1 2
MyFunc(7);
MyFunc(14, &sprintf, buffer);
Not quite the way you wanted...hard to do without using C++11 features, and even then it's not how you would want.
But why not this?
1 2 3 4 5
void MyFunc(int MyParam, std::string *s = 0)
{
//code
if (s) *s = (std::ostringstream() << MyParam).str();
}
1 2 3
MyFunc(7);
std::string str;
MyFunc(14, &str);
Last edited on Feb 7, 2012 at 11:08pm UTC
Feb 7, 2012 at 11:57pm UTC
Thanks for the replies,
Just wondering is it possible to do this in c++
Like concatenate strings at function parameter level for example.
_log(current_time += this->get_some_variable, para2,para3);
rather then have sprintf function as parameter.