+ 3
How can I make a number an object instead of a number in C++?
I want to do this.... int p = 5; cout << p * 5; I want the result to be: 55555 How can I do it?
3 Answers
+ 11
I guess you could make it a string object and overload * to work with strings like you want it to? but this begs the question as to why would you want to do this? couldn't you just make a function like this
string duplicateChar(char toDupe, int times)?
0
#include <string>
Function:
int join(int a, int b) {
//convert to string
string strA = to_string(a);
string strB = to_string(b);
//back to int
return stoi(strA + strB);
}
int p = 5;
p = join(p, 5);
//55
p = join(p, 5);
//555...
stoi and to_string are in the string library. They convert to and from string.