Thread: Overloading assignment operators

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Overloading assignment operators

    I've had a bit of a google for overloading the assignment operator, and it seems pretty decent. I'm a bit confused about using constant references and all that but I'll learn. What I'd like to know is how to overload with a common data type. For example, I'd like to overload "char *" so that assigning a static string to a char pointer just copies the text. Ie:

    Code:
    char *chTemp=new char[256];
    chTemp="Text";
    delete [] chTemp;
    I know I'll run into problems here, seeing as chTemp now points to a location within module memory (or whatever it's called), and I'm attempting to delete a block that is somewhere else.

    I know I can just use strcpy(), but I seems such a waste of time. Could anyone show me how to overload "char *" for this purpose?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Primitive or built-in datatypes cannīt be overloaded.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Isn't there some way I could duplicate the type, or make a class that operated in the same way? I don't mind specifying a different type instead of char *, as long as it acts the same way(except for what I'm trying to change!). Ie:

    Code:
    bStr=(bStr)new char[256];
    bStr="Text";
    delete [] bStr;
    Any chance?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Primitive or built-in datatypes cannīt be overloaded.

    What he said.


    >> I'm a bit confused about using constant references and all that but I'll learn.

    When your argument will not (and should not) be modified, then you use a const reference. It will protect the original data from being changed, and remove the overhead of copying. Also, if a user-defined object is not copyable (private copy cinstructor and operator=), then it cannot be passed by value, and must be passed by reference.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by bennyandthejets
    Isn't there some way I could duplicate the type, or make a class that operated in the same way? I don't mind specifying a different type instead of char *, as long as it acts the same way(except for what I'm trying to change!). Ie:

    Code:
    bStr=(bStr)new char[256];
    bStr="Text";
    delete [] bStr;
    Any chance?
    You could define your own string type.... or, you could use the std::string class in <string>
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM