I have a (little) Problem. I was going to program a little string class in C++, which should allow you to use a string in the same way as a string in C#.
I wrote the following class:
class STRING
{
private:
char * data;
int length;
public:
STRING();
STRING(char * data);
//string(char[] data, int length);
void setData(char * data);
int Length();
char * toString();
STRING operator = (STRING data);
STRING operator = (char * data);
STRING operator = (char data);
STRING operator = (const string data);
STRING operator + (STRING data);
friend ostream& operator<<(ostream&os, const STRING& data);
void display();
};
It is working. But I have one problem. If I try to initiate a new object of my class and want to set it with a value like:
STRING test1;
test1 = "TEST";
The compiler give me the following warning:
warning: deprecated conversion from string constant to ‘char*’
[-Wwrite-strings] test1 = "TEST";
So I try to get a solution in form of an operation overloading. I tried this:
STRING STRING::operator = (const string data)
{
char * tmp;
int laenge;
tmp = (char *) data
laenge = strlen(tmp);
this->data = new char[laenge + 1];
this->length = laenge;
strcpy(this->data, tmp);
}
But it dont work and I'm out of ideas. Of cause it would work if I initiate it with:
STRING test1;
test1 = (char *) "TEST";
but my Dream is to use it like: STRING test1; test1 = "TEST"; or STRING test1 = "TEST";