Okay, so I'm working on a C++ program meant to help Python and Ruby programmers learn C++, and I'm running into some trouble with the Ruby to_i and to_f functions. Here's my code:
int to_i(string number) { try { int i; std::stringstream(number) >> i; return (i); } catch(...) { throw "Function to_i requires string."; } }
What I'm trying to do is make it so that to_i can take both strings and floats. Is there some way I can do this, or do I have to make a different function? Also, is it possible to make this work like in Ruby, so instead of typing
to_i(myString);
you can type
myString.to_i();
?
Last edited by maxskywalker (2012-01-05 12:12:00)
Offline
You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.
Offline
poopo wrote:
You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.
Hm, not understanding this. Do you know any other languages that you could write it in?
Offline
define two methods: one called to_i(string mystring), and one called to_i(float myfloat)
you have to program them separately, though.
Offline
samtwheels wrote:
define two methods: one called to_i(string mystring), and one called to_i(float myfloat)
you have to program them separately, though.
And C++ will figure out which is which for me? Cool! It's not that long a function, and if it was, I could use goto.
Offline
You should ask Cheddargirl, she knows C++
Offline
maxskywalker wrote:
poopo wrote:
You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.Hm, not understanding this. Do you know any other languages that you could write it in?
What I mean is that there would have to be a method in the String class that was to_i()
for it to be called as:
myString.to_i();
instead of:
to_i(myString);
So you could add this code to the String class(this would be how it would be in Java):
public String to_i()
{
return myClass.to_i(this);
}
So you call it like this:
String myString = "2";
int myInt
myInt = myString.To_i();
That should work I think.
Offline
poopo wrote:
maxskywalker wrote:
poopo wrote:
You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.Hm, not understanding this. Do you know any other languages that you could write it in?
What I mean is that there would have to be a method in the String class that was to_i()
for it to be called as:
myString.to_i();
instead of:
to_i(myString);
So you could add this code to the String class(this would be how it would be in Java):
public String to_i()
{
return myClass.to_i(this);
}
So you call it like this:
String myString = "2";
int myInt
myInt = myString.To_i();
That should work I think.
Oh. Thanks.
Offline