I am starting a new project on classes with dynamic memory and I am trying to make sure that I correctly understand the set up of the constructor.
The instructions:
Construction of a MyString from a const c-string. You should copy the string data, not just store a pointer to an argument passed to the constructor. Constructing a MyString with no arguments creates an empty MyString object (i.e. ""). A MyString object should be implemented efficiently (space-wise) which is to say you should not have a fixed-size buffer of chars, but instead allocate space for chars on an as-needed basis. Use strcpy().
I am not sure of the differences between, or how it would look, "copy" and "store as a pointer." Here is the code that I started. I went this route because it is similar to what the instructor has done but it uses a pointer so I am unsure about that based on the instructions and I get this error.
error C2679: binary '<<': no operator found which takes a right-hand operand of type 'cs_mystring::MyString' (or there is no acceptable conversion)
#include <iostream>
#include <cstring> // for strcpy()
usingnamespace std;
class po{
public:
char ss[100]; //its size must be bigger than size of source
po(){
ss[0]='\0';
}
po(constchar *str){
strcpy(ss, str);
}
};
int main () {
char line[]="it's a line.";
po obj=line;
cout << obj.ss;
return 0;
}