Class design - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28

Thread: Class design

  1. #16
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Quote Originally Posted by GCDEF View Post
    Just change the return type to MyString&

    Your double pointer code should work. Did you try it?
    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?

    Quote Originally Posted by GCDEF View Post
    One other thought. Your operator [] should probably just return a char, not a MyString and it should check that the int passed in is valid.

    Also, you're creating a temporary MyString, but not assigning it a value, then attempting to access elements of s, which is meaningless since it has no value yet.
    Changed, thanks.

    Quote Originally Posted by Lindley View Post
    Your present approach of using a fixed-size char array is better than what you had in the first post, but it's not ideal. You haven't done any checks for whether the passed string will fit in the array you have available, and your object will be much larger than necessary for small strings.

    What you really want to do is dynamically allocate the memory necessary to copy the passed in string. However, doing this brings along several additional responsibilities:
    1) You must release the allocated memory in the destructor.
    2) You must ensure you allocate a different block of memory when copying, not just copy the pointer. (Unless you want to do reference counting, but that's even more difficult.)
    3) If your string is reassigned to be longer, you must modify your allocation safely to be able to handle the new value.
    Thank you, I will make those changes as soon as my program runs on a simple input.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    The main idea is for me to learn how to code it correctly so next time is won't be "something that is buggy",
    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:
    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();
    }
    The output should be:
    Code:
    abc
    abc
    abcdef
    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.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 21st, 2011 at 05:36 PM.

  3. #18
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    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:
    Code:
    Mystring** pp =p; // NOT: Mystring** pp =&p;
    
    Print(*ppr); // print is defined Print(char*)
    I really appreciate your help, it really important to me to know how to do it if it is possible.
    Last edited by BanKuZ; January 22nd, 2011 at 04:38 AM.

  4. #19
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Class design

    Is there a way to implement operator in Mystring so those 2 lines work properly:
    There is no way, and it is a good thing that it doesn't work.

    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:
    Code:
    Mystring** p;
    MyString* p2;
    p = p2;
    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.

    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:
    Code:
    Mystring** p;
    MyString* p2;
    p = &p2;
    So why are you trying to defeat this?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 22nd, 2011 at 04:46 AM.

  5. #20
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    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.
    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
    Last edited by Paul McKenzie; January 22nd, 2011 at 04:50 AM.

  6. #21
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    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?

  7. #22
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,093

    Re: Class design

    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?

  8. #23
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Quote Originally Posted by GCDEF View Post
    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?
    I tried to deference it twice, I didn't manage to do it.
    And it is something like homework only it was given by a C++ professional.
    Last edited by BanKuZ; January 22nd, 2011 at 06:44 AM.

  9. #24
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,093

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    I tried to deference it twice, I didn't manage to do it.
    And it is something like homework only it was given by a C++ professional.
    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.

  10. #25
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Quote Originally Posted by GCDEF View Post
    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.
    Well it is, and I think you're probably right. I just want to be 100&#37; positive I'm not missing anything before I do tell them such an answer.

    Thank you very much, you've been very helpful.
    Last edited by BanKuZ; January 22nd, 2011 at 09:24 AM.

  11. #26
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Quote Originally Posted by GCDEF View Post
    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.
    Can you help me with it? I tried several ways but couldn't make it work.

  12. #27
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,093

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    Can you help me with it? I tried several ways but couldn't make it work.
    If you can't modify the main function, there's no way I know to help.

  13. #28
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Quote Originally Posted by GCDEF View Post
    If you can't modify the main function, there's no way I know to help.
    Thanks anyway, you still helped a lot.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center