Hello,
for one of my college projects I am asked to create a basic string class to call upon in my code. I feel like I am very close, but something is not working correctly.
The c_str() is what is flagging an error. It says that in class "Mystring" there is no member for "c_str".
Which means I need to have it in my class to function, I am just unsure what to change to have it work correctly.
On a side note, I also noticed that removing the "Move constructor &&" from the code seemed to fix a lot of errors.
Is a move constructor necessary, I do not understand its purpose.
What is the error you run into? The purpose of a move constructor is to extract the contents of the mystring&& object into the object being created. It repurposes the pointer instead of copying the contents of what the pointer points to, thereby being more efficient. The mystring&& object should no longer be used afterwards, and will be destroyed when it goes out of scope.
PPS: I can almost guarantee you that it's bad practice for your generic Mystring class to have to be 'friends' with a specific class like Potion. You shouldn't have to do something like that. You should let the necessary functionality of your string be exposed through functions, just like how std::strings don't need to be friends with a Potion to be useful.
The default operator()= is not suitable when dynamic memory is used (as here). Hence the =delete to prevent run-time issues. If = is required, then a proper implementation for the operator=() function needs to be implemented.
This is quite easy to implement. In my code above replace L15-16 with:
Thank you for the resource, I will read up on noexcept and try to become more familiar with the operators. I do not fully understand your explanations, but I am fairly new to constructors/destructors and classes. So reading up on those will do me good.
Got it working correctly I think. At least the output is what I want.
I was receiving LINK 2005 errors, so I tried placing all of MyString functions inside the class instead of defining them outside of it. That seemed to work.
I also needed to place my char * str in public and not private so everything could access it.
heh we just had a discussion about that. Making stuff private just so you have to write getters and setters .. be practical was what most people said. Purism would demand that you do that (private with accessors) but purist code isnt practical for all things. Making the variable public is fine if you are willing to own the consequences (mostly, a user fooling with the variable directly in bad ways, eg setting it to nullptr and leaking memory that should have been deleted first for a simple example).
and, also, depends on if this is homework (what is your professor going to want) or learning (did you learn what you wanted from it) or whatever? I am going out on a limb and assuming its not useful (c++ has a better string built into it, no offense :) )
I'm going to take the purist approach here and say that there is no reason that str needs to be public. Making it public violates the encapsulation objective of C++.
Line 10 causes a memory leak. Line 5 does a new. Line 10 wipes out the pointer to the memory allocated at line 5. This is exactly the reason that str should not be public.
If you needed access to the str as a type const char* to pass as a pointer to another function (eg strcpy), then the way to do this is to provide a custom type overload within the class (or as a separate function as per Ganado's post above):
Rather than having Mystring convert to const char* implicitly I think it would be better to use a member function. That's how std::string works. There you call c_str() to get a const char*.