overloading =

This is a discussion on overloading = within the C++ Programming forums, part of the General Programming Boards category; My goal is to have a class with a char member and be able to say "charclass = myChar" when ...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    overloading =

    My goal is to have a class with a char member and be able to say "charclass = myChar" when charclass is some sort of class that has a char member and myChar is a char. I tried to make the return type of the operator function void, but I get errors. Please help.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    operator= should be a member in your class, it should take 1 parameter, the l-value of the assignment.
    something like:
    Code:
    myClass& operator=(char myChar);
    return *this to be able to use more than one assignment in the same statement.

    EDIT:
    Why not post what you have done so far so we can help more?
    Last edited by glUser3f; 12-22-2003 at 09:03 PM.

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    My code

    The program I'm trying to make uses a linked list to store stuff. I want to overload the = operator so that I can easily assign strings and numbers to my linked list. I'm confused. I don't understand why you have to return something in an operator function. I would greatly appreciate it if you could try to clear that up for me.

    Here's the code for the classes used in the linked list program (it's all I have so far):

    Code:
    class node
    {
    private:
    	char ch;
    	node * next;
    	node * prev;
    public:
    	friend class linkedlist;
    	node();
    };
    node::node()
    {
    	next = 0;
    	prev = 0;
    }
    
    
    class linkedlist
    {
    private:
    	node * current;
    	node * head;
    public:
    	linkedlist();
    	void createNext();
    	void gotoNext();
    	void gotoPrev();
    	void gotoHead();
    };
    linkedlist::linkedlist()
    {
    	head = new node;
    	current = head;
    }
    void linkedlist::createNext()
    {
    	current->next = new node;
    	current->next->prev = current;
    }
    void linkedlist::gotoNext()
    {
    	current = current->next;
    }
    void linkedlist::gotoPrev()
    {
    	current = current->prev;
    }
    void linkedlist::gotoHead()
    {
    	current = head;
    }

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The reason you'd want to return something is so that the operator works intuitively (the way it works for primitive types). For example:

    Code:
    node a, b, c(...);
    a = b = c; // Sets both a and b to whatever c is... Not possible if '=' returns void.
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I don't understand why you have to return something in an operator function.
    You don't have to, it's your class, you can make it do whatever you want. However, most clients will expect the overloaded operators to perform in a similar manner to the builtin operators for C++. For example, one could reasonably expect to assign your list class like so:
    Code:
    list1 = list2 = list3;
    They would expect such behavior because C++'s builtin operator= allows it. If you don't want to allow the above or anything similar, you can return void and document the behavior. Your class will still be correct within the domain you define for it.
    My best code is written with the delete key.

  6. #6
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    thanks for your help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Overloading operator ==
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 04:26 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 05:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21