0

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";

2
  • 4
    This is broken in too many ways to address in one post. Better learn some more C++ first. Commented Jun 17, 2015 at 15:20
  • I don't think you can cast the c++ string datatype as a pointer to char array, i.e. "tmp = (char *) data" won't work. Have a look at cplusplus.com/reference/string/string Commented Jun 17, 2015 at 15:23

3 Answers 3

5

You need an overload that takes a const char*

STRING STRING::operator = (const char* s)
{
    if (data != nullptr)
        delete [] data;
    length = strlen(s);
    data = new char[length + 1];

    strcpy(data, s);
}
Sign up to request clarification or add additional context in comments.

5 Comments

please bare in mind that this solution is prone to memory leaks.
@Pandrei I fixed the memory leak issue.
STRING s; s = nullptr; will crash your implementation in the strlen call.
@utnapistim: It may not. But it'll be UB regardless.
@utnapistim You can always "= delete" an overload taking an std::nullptr_t. The caller can still pass a null char*, but at that point the caller is responsible for the Undefined Behavior
1

You Need an overloaded function for const char* but you can also use a template function which will be faster:

template<int length>
STRING STRING::operator = (const char (&s)[length])
{
    //resize or allocate data if needed
    strncpy(data, s, length);
}

2 Comments

Faster and far less flexible.
Should be const char (&s)[length]. Otherwise it will be taking an array of references.
0

You can change the argument types from char* to char const* in the following functions.

STRING(char * data);
STRING operator = (char * data);

Also, as a matter of general improvement, you can change

STRING operator = (STRING data);
STRING operator = (const string data);
STRING operator + (STRING data);

to

STRING& operator = (STRING const& data);
STRING& operator = (const string& data);
STRING operator + (STRING const& data) const;

See http://en.cppreference.com/w/cpp/language/operators why my suggested prototypes are more appropriate than what you have.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.