Followers 0

Make a int that could be passed as string

14 posts in this topic

Hello.

Could you provide some directions how would i make a class that would be a int holder. but i could retrieve data as "int" or "std::string" using overloads

exzample

my_custom_int value;
value = 0; //valid
value = 's';//invalid
value = "asd";//invalid

std::string string;
string = "asd"; //valid
string = value; // this to be valid


The last case to be valid what i need to do?

0

Share on other sites

Note how the standard library could allow such a conversion from std::string to const char *, but chooses not to. Conversion operators are hidden and can introduce subtle bugs. Providing an explicit str() method would be preferred.

That would be good enough.

What i don't know what i am looking for, in term of programing.

I cant find with Google a tutorial that would show how to make a "class int" with all functions it currently poses.

I really hate do make std::stringstream name.

then feed it int data.

then use it as .str()

then when i want to reuse it i need to clean it.

example

//I want
my_int a = 0;
a = unit.attack_power;
my_text.setText(a.str());

//I have
my_int a = 0;
a = unit.attack_power;
std::stringstream conv_attack_power;
conv_attack_power << a;
my_text.setText(conv_attack_power.str());



EDIT:: Note i don't want to replace the standard int i want to make a new class when i require this kind of usage.

Edited by BaneTrapper
0

Share on other sites
Then either write one conversion helper functions somewhere in your Utils.h or some such (which you will need sooner or later anyway) or use boost::lexical_cast?
0

Share on other sites

Helper functions will simplify even further:

void setNumber(TextWidget &widget, int number) {
// As BitMaster says, this int/string conversion logic could be moved elsewhere
std::stringstream stream;
stream << number;
widget.setText(stream.str());
}

// Later
setNumber(my_test, unit.attack_power);

Edited by rip-off
2

Share on other sites

Helper functions will simplify even further:

void setNumber(TextWidget &widget, int number) {
// As BitMaster says, this int/string conversion logic could be moved elsewhere
std::stringstream stream;
stream << number;
widget.setText(stream.str());
}

// Later
setNumber(my_test, unit.attack_power);


i am thinking about something more like

std::string int_to_string(int value)
{
std::stringstream conv;
conv << value;
return conv.str();
}

// usage
OutputText(unit.name + " " + int_to_string(unit.attack_power) + "is over 9000");


I have so much happening and thinking about so many stuff.
Much thanks on help!

1

Share on other sites

value = 0; //valid
value = 's';//invalid


I see a problem with these two lines. How do you have a class that accepts 0 (an int) and rejects 's' (an int)?

0

Share on other sites

value = 0; //valid
value = 's';//invalid


I see a problem with these two lines. How do you have a class that accepts 0 (an int) and rejects 's' (an int)?

's' is a char in C++, so you'd need a function that takes a char and produces an error when passed something other than a digit. Or just always produces an error.

0

Share on other sites

C++11 introduced some new conversion functions for that exact purpose:

std::to_string() - Integers and float-types to std::string.

Also:

std::stoi() - std::string to int

std::stol() - std::string to long

std::stoul() - std::string to unsigned long

std::stof() - std::string to float

std::stod() - std::string to double

The function-name format is:

stof

s <to> f

string <to> float

Just like the pre-existing atoi() (where 'a' is for ASCII, 'char*' raw-string functions)

This is different from strtof() and the like. I have no clue what those functions do.

([b][Edit:][/b] Ah, here's an explanation of strtof. I never knew about that function)

Edited by Servant of the Lord
2

Share on other sites

C++11 introduced some new conversion functions for that exact purpose:

std::to_string() - Integers and float-types to std::string.

Also:

std::stoi() - std::string to int

std::stol() - std::string to long

std::stoul() - std::string to unsigned long

std::stof() - std::string to float

std::stod() - std::string to double

The function-name format is:

stof

s <to> f

string <to> float

Just like the pre-existing atoi() (where 'a' is for ASCII, 'char*' raw-string functions)

This is different from strtof() and the like. I have no clue what those functions do.

([Edit:] Ah, here's an explanation of strtof. I never knew about that function)

Inventing a hot water? Nah, id just use this...

Rereading c++11 is a good idea i see, thanks much.

0

Share on other sites

Also boost::lexical_cast is nice for these kinds of things.

0

Share on other sites

Remember! More code just takes up more memory, which is very plentiful these days, if anything you only want to be worried about usability and performance on critical sections, so if something seems messy, write more code(to an extent anyway.)

That or write cleaner feeling code.

1

Share on other sites

value = 0; //valid
value = 's';//invalid


I see a problem with these two lines. How do you have a class that accepts 0 (an int) and rejects 's' (an int)?

's' is a char in C++, so you'd need a function that takes a char and produces an error when passed something other than a digit. Or just always produces an error.

Ah, you're right. Character constants became of char type in C++. My mistake.

0

Share on other sites

Yep, it's one of the subtle differences between C and C++. It means that code like this will have different results in the two languages.

int main(){
return sizeof('a');
}
0

Create an account

Register a new account

Followers 0

• 28
• 13
• 11
• 31
• 20