Yeah, I was really only confused about
Code:
my_String& operator=(const my_string &object);
and
Code:
operator char*() {return ptr;}
I figured giving you guys some of the code would help explain what these were. Like i said I'm not used to posting here, and since I don't fully understand what I'm asking, I don't know what is relevant or not.
Thank you for explaining my_string& for me, I really appreciate it!
I'm still unsure how operator char* works, or what parts of the code are relevant to showing you where it is involved or not. So I copied down the entire code.
I'm new so I'm unsure if all of this is even needed or not. If it isn't needed then I apologize for pasting the following monolith of code.
Code:
class String {
private:
char *ptr;
public:
String();
String(char *s);
String(const String &src);
~String();
String& operator=(const String &src)
{cpy(src.ptr); return *this;}
String& operator=(char *s)
{cpy(s); return *this;}
String operator+(char *s);
int operator==(const String &other);
operator char*() {return ptr;}
void cat(char *s);
void cpy(char *s);
};
int main() {
String a, b, c;
a = "I ";
b = "am ";
c = "so ";
String d = a + b + c + "very happy!\n";
cout << d;
return 0;
}
// ----------------------------------
// STRING CLASS FUNCTIONS
String::String() {
ptr = new char[1];
ptr[0] = '\0';
}
String::String(char *s) {
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
String::String(const String &src) {
int n = strlen(src.ptr);
ptr = new char[n + 1];
strcpy(ptr, src.ptr);
}
String::~String() {
delete [] ptr;
}
int String:: operator==(const String &other) {
return (strcmp(ptr, other.ptr) == 0);
}
String String::operator+(char *s) {
String new_str(ptr);
new_str.cat(s);
return new_str;
}
// cpy -- Copy string function
//
void String::cpy(char *s) {
delete [] ptr;
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
// cat -- Concatenate string function
//
void String::cat(char *s) {
// Allocate sufficient room for new string data.
int n = strlen(ptr) + strlen(s);
char *p1 = new char[n + 1];
// Copy data to this new memory block.
strcpy(p1, ptr);
strcat(p1, s);
// Release old memory block and update ptr.
delete [] ptr;
ptr = p1;
}