CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: Class design

  1. #1
    Join Date
    Jan 2011
    Posts
    13

    Class design

    Hi all, I have this class in my code:
    class Mystring {
    public:
    char *s;
    Mystring(const char *);
    Mystring();
    ~Mystring(){}

    Mystring operator=(const char* str)
    {

    strcpy (s, str);
    return *this;
    }
    operator char *()//Conversion operator
    {
    return this->s;
    }
    //...
    };

    1) I would like to convert from Mystring* to Mystring** using a '=' operator. for this line to work:"
    Mystring* p = new Mystring;
    Mystring** pp = p;

    2) And to convert from Mystring* to char* using a conversion operator (I already have one in my class).

    I'm having difficulties with both issues, Thanks for any help!!
    BanKuZ
    Last edited by BanKuZ; January 21st, 2011 at 01:57 PM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Class design

    The first doesn't make a whole lot of sense.

    The second is easy; just use the MyString object anywhere a char* is expected, or do an explicit static_cast. However, it's worth noting that implicit conversions are a controversial subject; some say they're a bad idea.

  3. #3
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Thank you,
    But that's exactly my problem,
    For the first one I'm trying to avoid changing any lines in the main function (since it was given to me).
    And for the second one I can't use the MyString object, just a pointer for the object for the same reason.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    Thank you,
    But that's exactly my problem,
    For the first one I'm trying to avoid changing any lines in the main function (since it was given to me).
    Show the main function then so we can figure out what that line is trying to do.

    And for the second one I can't use the MyString object, just a pointer for the object for the same reason.
    So dereference the pointer.

  5. #5
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Here is the relevant part of the main function:

    int main (void) {

    Mystring* p = new Mystring();

    *p = "aaa";
    Mystring** pp =p;
    Print(*pp);//defined as Print(char*);
    //...
    Print(*p); // this line currently work
    //...

    I tried to dereference the pointer in several ways - none worked, can you show me how?
    Last edited by BanKuZ; January 21st, 2011 at 02:00 PM.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Class design

    I'm not following why you think you need a **

    I'm not seeing where you're allocating memory for your internal char* either.

    It's not clear what name is.

    Your char* operator should return s.

    Please use code tags when you post code.
    Last edited by GCDEF; January 21st, 2011 at 12:09 PM.

  7. #7
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Quote Originally Posted by Lindley View Post
    Show the main function then so we can figure out what that line is trying to do.

    So dereference the pointer.
    Quote Originally Posted by GCDEF View Post
    I'm not following why you think you need a **

    I'm not seeing where you're allocating memory for your internal char* either.

    It's not clear what name is.

    Your char* operator should return s.

    Please use code tags when you post code.
    HI GCDEF,

    The main function is a given, that's the only reason I need a **.
    I didn't have any problem for not having a memory allocation for the internal char* why is it important? should I add it to the constructors?
    You are right about my char* operator, it is now fixed. thanks!

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    HI GCDEF,

    I didn't have any problem for not having a memory allocation for the internal char* why is it important? should I add it to the constructors?
    You're writing to unallocated memory. The most likely result of that is a crash.

    The whole pointer pointer thing makes no sense in that program, but why not just do it the normal way?

    Mystring** pp = &p;

    Also, your assignment operator should return a MyString reference, not a MyString.

    Code:
    Mystring& operator=(const char* str)
    {
    // you still need to free existing memory and allocate new memory here.
         strcpy (s, str);
         return *this;
    }
    Last edited by GCDEF; January 21st, 2011 at 02:22 PM.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    HI GCDEF,

    The main function is a given, that's the only reason I need a **.
    I didn't have any problem for not having a memory allocation for the internal char* why is it important? should I add it to the constructors?
    You are right about my char* operator, it is now fixed. thanks!
    The obvious question is why you just don't use std::string? All of this work is taken care of already for you.

    If you had this trouble in figuring out how to properly handle dynamically allocated memory, then it is more of a reason to use standard classes instead of trying to code something that is buggy.

    Secondly, your code will fail here:
    Code:
    int main()
    {
        Mystring s("abc");
        Mystring s2 = s;
    }
    Just that simple two line program, given what you've coded, has an error when main() exits. You need an assignment operator and copy constructor. Again, more of a reason to use std::string.

    Edit: I don't see your class doing any dynamic allocation whatsoever, so what is the purpose of this class? How about posting your class implementation, so we know what you're trying to do.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 21st, 2011 at 02:34 PM.

  10. #10
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Code:
    class Mystring {
    
    public:
        
        char s[MAX_SIZE];
    
        Mystring(){}
        
        Mystring (const char* str)
        {
            strcpy (s, str);
        }
    
        Mystring (const Mystring & Astr)
    	{
    		 strcpy (s, Astr.s);
    	}
        ~Mystring(){}
        
        Mystring operator=(const char* str)
        {
            strcpy (s, str);
            return *this;
        }
    
        Mystring** operator=( Mystring* Astr) /// didn't solve 
        {
    		Mystring** ppTemp= &Astr;
    		return ppTemp;
        }
     
        Mystring operator+(Mystring CMy)
        {
            Mystring temp;
            strcpy (temp.s, s);
            strcat (temp.s, CMy.s);
            return temp;
        }
        
        Mystring operator += (const char* str)
        {
            strcat (s, str);
            return *this;
        }
    
        Mystring operator [] (const int n)
        {
            Mystring temp;
            temp.s[0]=s[n];
            temp.s[1]='\0';
            
            return temp;
        }
        
       
        operator char *()//Conversion operator
    	{
    
    	
            return this->s; 
        }
    	operator Mystring **()//Conversion operator - didn't solev
    	{
            return *this; 
        }
        int len ()
        {
            int tmp_len;
            tmp_len = strlen(s);
            return tmp_len;
        }   
                
    };
    That's what I wrote so far, I have problem with the lines that I mentioned before. As I said the MAIN is given and I trying to avoid changing it's syntax.
    I'm more then happy to here remarks about how to make my code better.
    I didn't fully understand your last remark about how I should use memory allocation, and of course std::string is not an option for me. The main idea is for me to learn how to code it correctly so next time is won't be "something that is buggy",
    I really appreciated your help,
    BanKuZ

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Class design

    Again, anywhere where you're returning *this, your return type needs to be a MyString&.

    The whole ** thing just doesn't make sense. As far as I know, there's no way to do it. When you're operating with pointers such as
    MyString** pp = p, you're not even dealing with your MyString object. You're just assigning one memory address to another in a way that doesn't make sense and won't work.

    You're telling the compiler that pp will point to a MyString pointer, then assigning it to an address that points to a MyString object. It's a nonsensical concept that you should strop trying to implement.

  12. #12
    Join Date
    Jan 2011
    Posts
    13

    Re: Class design

    Quote Originally Posted by GCDEF View Post
    Again, anywhere where you're returning *this, your return type needs to be a MyString&.
    Can you specify what to write instead?

    Quote Originally Posted by GCDEF View Post
    The whole ** thing just doesn't make sense. As far as I know, there's no way to do it.
    O.K I appreciate it, I don't know either that's why I asked.

    Is there a way to use conversion cast for casting from Mystring* to char*?
    Like if I have a function defined as: func (char*) and I would like to use it this way:
    Code:
       
    Mystring** pp =&p;
        Print(*ppr);

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Class design

    Just change the return type to MyString&

    Your double pointer code should work. Did you try it?

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Class design

    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.
    Last edited by GCDEF; January 21st, 2011 at 08:18 PM.

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Class design

    Quote Originally Posted by BanKuZ View Post
    I didn't fully understand your last remark about how I should use memory allocation
    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.

Page 1 of 2 12 LastLast

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
  •  





Click Here to Expand Forum to Full Width

Featured