Sign in to follow this  
BaneTrapper

Make a int that could be passed as string

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Personally, I'd advise against automatic conversion whenever possible.

Still, if you are dead set on it you should store the int in a class and add a conversion operator

class MyIntWrapper
{
public:
   MyIntWrapper():value(0) { }
   explicit MyIntWrapper(int value):value(value) { }

   operator int() const { return value; }
   operator std::string() const { return boost::lexical_cast<std::string>(value); }

private:
   int value;
};

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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!

Share this post


Link to post
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.

Share this post


Link to post
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. smile.png

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

Edited by Servant of the Lord

Share this post


Link to post
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. smile.png

([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.

Share this post


Link to post
Share on other sites

As was pointed out, helper functions are your friend or even just overloading functions to return different types depending on your needs.

 

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this