Make a int that could be passed as string

Started by
13 comments, last by King Mir 11 years, 2 months ago

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?

Advertisement
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;
};

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.

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.

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?

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);

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!



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)?


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.

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)

This topic is closed to new replies.

Advertisement