Jump to content

  • Log In with Google      Sign In   
  • Create Account


Make a int that could be passed as string


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1097

Like
0Likes
Like

Posted 15 February 2013 - 04:15 AM

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?


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3609

Like
3Likes
Like

Posted 15 February 2013 - 04:47 AM

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


#3 rip-off   Moderators   -  Reputation: 7622

Like
3Likes
Like

Posted 15 February 2013 - 04:49 AM

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.



#4 BaneTrapper   Members   -  Reputation: 1097

Like
0Likes
Like

Posted 15 February 2013 - 05:21 AM

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, 15 February 2013 - 05:36 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#5 BitMaster   Crossbones+   -  Reputation: 3609

Like
0Likes
Like

Posted 15 February 2013 - 05:38 AM

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?

#6 rip-off   Moderators   -  Reputation: 7622

Like
2Likes
Like

Posted 15 February 2013 - 06:09 AM

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, 15 February 2013 - 06:10 AM.


#7 BaneTrapper   Members   -  Reputation: 1097

Like
1Likes
Like

Posted 15 February 2013 - 07:34 AM

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!


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#8 Ectara   Crossbones+   -  Reputation: 2743

Like
0Likes
Like

Posted 15 February 2013 - 04:28 PM


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



#9 King Mir   Members   -  Reputation: 1889

Like
0Likes
Like

Posted 15 February 2013 - 09:40 PM

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.



#10 Servant of the Lord   Crossbones+   -  Reputation: 16686

Like
2Likes
Like

Posted 15 February 2013 - 10:15 PM

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)


Edited by Servant of the Lord, 15 February 2013 - 10:20 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#11 BaneTrapper   Members   -  Reputation: 1097

Like
0Likes
Like

Posted 16 February 2013 - 03:04 AM

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.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#12 japro   Members   -  Reputation: 887

Like
0Likes
Like

Posted 16 February 2013 - 03:29 AM

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



#13 Satharis   Members   -  Reputation: 945

Like
1Likes
Like

Posted 16 February 2013 - 06:07 AM

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.



#14 Ectara   Crossbones+   -  Reputation: 2743

Like
0Likes
Like

Posted 16 February 2013 - 09:19 AM

 

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.



#15 King Mir   Members   -  Reputation: 1889

Like
0Likes
Like

Posted 17 February 2013 - 03:49 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS