I'm writing string class on my own. And I have such code. I just want to overload operator=
. This is my actual code, and I get error in last part of code.
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class S {
public:
S();
~S() { delete []string;}
S &operator =(const S &s);
private:
char *string;
int l;
};
S::S()
{
l = 0;
string = new char[1];
string[0]='\0';
}
S &operator=(const S &s)
{
if (this != &s)
{
delete []string;
string = new char[s.l+1];
memcpy(string,s.string,s.l+1);
return *this;
}
return *this;
}
But unfortunately I get error 'S& operator=(const S&)' must be a nonstatic member function.
memcpy
declared: liveworkspace.org/code/92ac98695817213f6c15af241904d165