Thread: Operator= overloading from another type

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Operator= overloading from another type

    Hi, I've got a problem a problem in overloading operator=. Well, I have little experience in overloading operators. How do I overload = operator from a different type of class?

    I've made this:
    Code:
    	inline CBitmapString& operator=(const string theString)
    	{
    		mString=theString;
    		mLength=countLength();
    		return *this;
    	}
    But the code is faulty. Can you help me please?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In what way is it faulty?

    It would be a good idea to make the argument a reference, but that's just a performance issue.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Well, if I do this:
    Code:
      CBitmapString*  mStringTipsGirl1;
      mStringTipsGirl1 = new CBitmapString;
      string tipsGirl;
      mStringTipsGirl1=tipsGirl;
    it returned this error:
    Code:
    error: cannot convert ‘std::string’ to ‘CBitmapString*’ in assignment

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    operator=() can only be a member of the class at the LHS of an expression. It is not possible to define an operator=() for basic types (int, float, double, ..... and any pointer). Your compiler is therefore complaining about an attempt to set a "pointer to CBitmapString" to something which is not a "pointer to CBitmapString".

    If you supply an operator=() which accepts a string argument as a member of your CBitmapString class (which I suspect you've done, but you haven't made that clear), you can do;
    Code:
        *mStringTipsGirl1=tipsGirl;

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Thanks a lot. Yeah, forgot about the pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. Changing an objects Type
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2004, 07:18 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM