Tried it, didn't work. Can you please write the code lines so I'll find out if I did it the way you meant?
Changed, thanks.
Thank you, I will make those changes as soon as my program runs on a simple input.
Printable View
Tried it, didn't work. Can you please write the code lines so I'll find out if I did it the way you meant?
Changed, thanks.
Thank you, I will make those changes as soon as my program runs on a simple input.
The issue is that you do not have a full grasp of pointers and how to properly handle dynamically allocated memory. Without this, you cannot create a string class.
Secondly, there shouldn't be a "next time" once you can code some string class. Unless there is a compelling reason to do so, you should use what is known to work correctly, and that is std::string. The std::string class and other C++ library classes have been written by some of the best C++ programmers in the business, so there is little (to be honest, no) chance you're going to do better than their version.
Also, I'm being very frank -- I have never seen a beginner programmer ever get a string class correct. That's why it takes (at the very least) an intermediate C++ programmer who is well-grounded with issues such as pointer manipulation, memory management, and other aspects to properly code a string class that actually works reasonably well with little, to no issues.
If you want to see how to put together a string class, look at the many sample programs that shows how to do it. If you want a skeleton, finish the implementation below:
The output should be:Code:#include <iostream>
class MyString
{
char *m_ptr;
public:
MyString();
MyString(const char* str);
MyString(const MyString& str);
~MyString();
MyString& operator=(const MyString& rhs);
MyString& operator=(const char* str);
MyString& operator += (const MyString& str);
MyString& operator += (const char * str);
const char* getData();
};
// sample program
using namespace std;
int main()
{
MyString s1("abc");
MyString s2 = s1;
MyString s3;
s3 = s2;
s3 += "def";
cout << s1.getData() << "\n" << s2.getData() << "\n" << s3.getData();
}
Can you start here and implement these functions in MyClass? Once you do that, then make sure that the main() function works correctly (correct output, no memory corruption on exit of main(), etc). Then we can clearly see what you know and what you don't know.Code:abc
abc
abcdef
Regards,
Paul McKenzie
I already did most of it in my code. I understand you are trying to help and it seems you have a lot of knowledge. I know the right way of doing it is what you all suggested, and I will change it.
The most helpful thing for me right now is to know if what I'm trying to do is even possible and what's the right way of doing it.
Is there a way to implement operator in Mystring so those 2 lines work properly:
I really appreciate your help, it really important to me to know how to do it if it is possible.Code:Mystring** pp =p; // NOT: Mystring** pp =&p;
Print(*ppr); // print is defined Print(char*)
There is no way, and it is a good thing that it doesn't work.Quote:
Is there a way to implement operator in Mystring so those 2 lines work properly:
If it did work, then your code would run counter to how C++ is supposed to behave. If any C++ programmer were to see this:
the programmer would expect a compiler error, since you cannot assign pointer types with different indirections this way. Thankfully, the compiler doesn't allow it either. To make member function calls, you need an object -- there is no object in the code above. You have a Mystring** on the left hand side, not a MyString object.Code:Mystring** p;
MyString* p2;
p = p2;
So the real question is why you want to write code that is counter to what a C++ programmer expects? Every C++ programmer knows this works:
So why are you trying to defeat this?Code:Mystring** p;
MyString* p2;
p = &p2;
Regards,
Paul McKenzie
If it even was possible, does it make it "right"?
For example, what if I defined a class where operator + does subtraction, and operator - did addition? Could I do it? Yes. Would it be "right"? Of course not.
Class design (the title of your thread) is a big topic, and requires a lot of thought way beyond whether something is syntactically possible in C++. If creating code makes the class harder to use, or could introduce bugs, or just confuse the heck out of any C++ programmer using your class, then the class design is faulty and shouldn't even be considered.
Regards,
Paul McKenzie
I agree with your answer, but I'm trying to build a class for a given main() function.
I'm trying to make to code given to me work correctly even if there are many other and better ways of coding the same thing, I have to stay with this main() - it's not for me to choose.
Are you saying it's impossible or is it just not right to do it this way?
Print(*ppr); // print is defined Print(char*)
That still won't work because ppr is a pointer to a pointer, so *ppr is just a MyString*. You need to be working with the object, not a pointer to use your operators. You'd need to deference it twice.
Who gave you such an odd main function? Is this homework?
That C++ "Professional" is in the wrong line of work. Assuming it's a test for a job, I'd just tell them that it couldn't be done and even if it could, it's a really bad idea. It's possible that's the answer they're looking for. Part of being a good programmer is being able to identify when you're headed down the wrong path and to stop.