Hello i made a variable storage like string
and this is the source
class str
{
private:
const char * _mystring;
public:
// Using this function to made new str
str(const char * _str) { _mystring = _str; }
// Get the content of this str
const char * read() { return _mystring; }
// Get the content in char *
char * read_c() { return strdup(_mystring); }
// Operator for equal action with extern const char
str & operator = (const char * _str) { _mystring = _str; return *this; }
// Operator for equal action with extern str storage
str & operator = (str _str) { _mystring = _str.read(); return *this; }
// Operator for add action
str operator + (const char * _str) {
return str(strcat(read_c(), _str));
}
// Operator for add action with new str
str operator + (str & _str) {
return str(strcat(read_c(), _str.read()));
}
};
and this is my test program
int main() {
str var1("Var1"); // No problem
str var2("Var2"); // No problem
str var3 = var1 + var2; // No problem
str var4 = var1 + "tempStr"; // I got runtime error !
str var5 = "tempStr" + var2; // I got error before compile
cout << var1.read() << endl;
cout << var2.read() << endl;
cout << var3.read() << endl;
cout << var4.read() << endl;
return 0;
}
what is the problem that i can't create something like var3 and var4 and i got error (i get error while i want to merge const char with my str ... i set + operator but there is problem for when i want to merge const char with my str (not my str with const char... on this action, there is no problem) but wait i got error (debug (close program)) after compile and print var7 too !