I need to pass part of a string to function:

string original_string="my_name_original";
string wanted_string (original_string,7); //take "my_name" only
my_func(wanted_string, other_var);
//func:
void my_func(string wanted_string, int other_var);

This doesn't seem to work, the compiling was OK but the passed "wanted_string" is empty; When pass char*, it works fine:

char* original_string="my_name_original";
my_func(wanted_string, other_var);
//func:
void my_func(char* wanted_string, int other_var);

Questions:
(1) How to get "my_name" from char* original_string?
(2) Is it true that string can NOT be passed?

Well, for starters you used the wrong constructor. Change the definition of wanted_string to this:

string wanted_string(original_string, 0, 7);

If that still doesn't work, post a complete program so we can test it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.