Thread: assignment operators

  1. #1

    Question assignment operators

    I'm writing code for a linked list object. The object uses a structure for the nodes called LinkNode. I want to write assignment operators for the LinkNode structure and for pointers to the data type that the LinkList object contains. Here's a little sample of y code:

    template <class Type>
    struct LinkNode
    LinkNode<Type> *prev;
    Type *dat;
    LinkNode<Type> *next;

    template <class Type>
    class LinkList
    //All kinds of crap
    LinkNode<Type> *first;
    LinkNode<Type> *current;
    LinkNode<Type> *last;
    int length;

    I want to be able to do something like this:

    LinkList<int> list;
    int *temp;

    temp = list;

    I want temp to then have the the address of the data in the current node of list. Does that make sense?

    I tried writing an operator like this:

    template <class Type>
    Type *operator = (Type *t, LinkList<Type> &l)
    t = l.GetData();
    return t;

    //repeat the same thing for LinkNode<Type>*

    That code gives me some error about the operator needing to be a "static member function", or something like that. Can you define the assignment operator outside of a class? If so, do I need to make the first parameter a reference (Type* &t)? Thanks for any help.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    You need to declare the function as a 'friend' within your class declaration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07: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. Overloading assignment operators
    By bennyandthejets in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2003, 09:29 AM