• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BaneTrapper

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 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
0

Share this post


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

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!

1

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

0

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.

0

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
2

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.

0

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.

1

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.

0

Share this post


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

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  
Followers 0