-1

There is this function :

void func(const char *)

and I wanna use it like :

DWORD dWrd = 19;
func("I am " + dWrd + " years old!")

thanks..

Okay, I got this another function very related to the question above, hope I don't violate any rules.

So what if I have this function that accepts an argument that it would print using cout and write to a file isinf fstream, what data type should I use?

void myFunc(char c) ?

void myFunc(const char c) ?

void myFunc(const char *c)?

2 Answers2

3

You could convert everything to a std::string then use c_str to get a const char*

func(("I am " + std::to_string(dWrd) + " years old!").c_str())

To be a bit more verbose (and avoid the implicit conversions)

func((std::string("I am ") + std::to_string(dWrd) + std::string(" years old!")).c_str())

Be careful regarding what you do with the const char* within func as once the function returns the temporary std::string will be destroyed and the pointer is no longer valid.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Use of std::ostringstream :

#include <iostream>
#include <string>
#include <sstream>

int main()
{
   DWORD dWrd = 19;
   std::ostringstream out;  
   out << "I am " << dWrd << " years old!";
   func(out.str().c_str());
}