Thread: Assigning a String object to char*

  1. #1
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    Assigning a String object to char*

    I want to overload the '=' operator so that I can assign a String
    object to char*. I am using a friend function.
    I'm not sure on how to do this but here's my code:
    Code:
    class String
    {
    public:
           String();
           String(String&);
           String(char*);
           virtual ~String();
           
           String&  operator =(const String&);
           String&  operator =(const char*);
           friend char* operator=(char*,String&);
           //.....
           //.....
    private:
           char  *lpzstr;
    };
    I get an error that says:

    'operator =' must be a <Unknown> member

    What does this mean AND/OR how would I go about
    accomplishing this task?

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    This line is funky

    friend char* operator=(char*,String&);

    I am 99% sure that this line makes no sense.

    first, a friend? What are you trying to make a friend? this is a member function on your object, you don't make it a friend.

    second, two parameters. I'm pretty certain that you can't do that either. Why do you want to anyway?

    I am going to guess that you don't even really want this line. the first two overloads of operator= seem to be all you want. and they appear correct at first glance.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, think about this:
    If I wanted to overload an operator so that the object is on the
    right of the operator, them I'll need to use a friend function.

    Now, what i want to do is to be able to assign a String object to
    a char*
    eg.
    Code:
    char temp[20];
    String one = "Hello World!!";
    temp = one; //Thus assigning an object to a char*
    If you perhaps know how to do this then i'll appreciate any help
    very much thanx.
    I am 99% sure that this line makes no sense.
    Could you please explain as to why this declaration is 'funky'

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    my two reasons were the parameter list and the "friend"

    friend is used for allowing global functions or other objects to reference your private data. I can't imagine it having any effect or being appropriate syntax on your own member function.

    second, I don't think operator= can have two parameters.

    That's funky

    Here's how you do what you are talking about:

    Code:
    class String
    {
    public:
           String();
           String(String&);
           String(char*);
           virtual ~String();
           
           String&  operator =(const String&);
           String&  operator =(const char*);
           operator char *();
    
    private:
           char  *lpzstr;
    };
    it may look completely wild but it works. operator char* will return the string and you will be super happy!

    String dummystring = "hi there";
    char *test = dummystring;

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Thanx Hershlag.

  6. #6
    Unregistered
    Guest
    If I were you I wouldn't perform string to c string assignment with the assignment operator at all. It violates the rules of object oriented programming; if you want to assign an instance of one class to another, the proper way to do it is to overload the assignment operator for the class that is receiving the assignment. In this case, since char* isn't a class, you can't do this, so for the sake of making your code more understandable and not violating unwritten OOP rules, you should perform this operation with a non-operator memember function. Most good string libraries come with a c_str() function for this exact purpose so that you could write:

    String var1 = "hello";
    char* var2 = var1.c_str();

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    operator char *();
    How would i define this?
    And does anyone know wtf it means?

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    In this case, since char* isn't a class, you can't do this
    The way I understand it is that if you can overload an operator
    so that the object is on the right of the operator, then why
    can't you assign an String object to char*?

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >The way I understand it is that if you can overload an operator
    so that the object is on the right of the operator<

    You can't do this with operator=. Or operators->,() and [].

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You can't do this with operator=. Or operators->,() and [].
    Only these operators?

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    listen, I can't argue that my suggestion is CORRECT. I just said that that's how you do it!

    Unregistered said that c_str() is the right way to get a const char * out of this thing and I am inclined to agree (probably cause that's what I'm used to seeing in the string class)

    if you want to do it though, the function would be defined as follows:
    Code:
    String:: operator char*()
       {
       return lpzstr;
       }
    this is kind of like overloading a cast operator. if you are casting your object to a char *, this will work. Your call though, I would just use a c_str() type thing. Actually, I would just use the stinking standard library string in the first place.

  12. #12
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Don't worry Hershlag, I wasn't trying to get in your face.

    I found what this is used for :
    Code:
    operator char *();
    It's used to implement implicit casting.
    And don't worry, it is CORRECT and there's nothing wrong
    with it.

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    hey dog, my semi-defensive reply was directed at "unregistered", not you. wasn't like I was hurt anyway

    by "correct", I meant the way that people talk about software design not including certain features of the language. like goto for instance. That's one I would most certainly agree with. global variables are another one that I could do without. anyway, the "overloaded cast" is one that some people don't like. That's all.

  14. #14
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Oh, my bad.
    [QUOTE]
    anyway, the "overloaded cast" is one that some people don't like.
    [/QUOTE[

    Well they can all goto hell. //just kidding
    I'm just happy I got my class working properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM