#### Archived

This topic is now archived and is closed to further replies.

This topic is 5278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

For the purpose of learning I''ve been working on a linked_list class. The linked list contains various methods, and a pointer to the head and a pointer to the tail which point to node objects. Each node has a pointer to data. Both linked_list and node are templated to hold any kind of data. Yes, I know the std exists. No, I don''t want to use it instead. I like learning how things work and creating something on my own. I will probably use it later, but for now, I would like to knwo how something like this works. Anyways. I''ve overloaded the subscript operator. I was hoping to get a more intuitive use out of the linked list. But using [] actually makes things more confusing. Its obvious I''m not implementing it correctly.
template <typename T>
if(subscript>members)
return NULL;
long start = 0;
while(start!=subscript){
cur_node=cur_node->next;
start++;
}
return cur_node->data;
}

I would like to return the data of the node, rather than the node itself as to create a layer of transparency. To use it, I have to use i=(&(*uvpoly_list)[n])->somememberofdata; Alas. VERY counter-intuitive. I''ve tried googleing "operator[]" However, all symbols are left out of searches so im limited to searching ''subscript operator'' I would search for operator-> but I have no idea what its name even is. :-/ Sorry for the long post. I tend to explain too much. Maybe someone can point me in the right direction. Thanks in advance.

##### Share on other sites
You shouldn''t return NULL there. It won''t work if 0 can''t be converted to type T. Instead, return T(). Other than that, it looks like you should just be able to do

// fill myList, and then
myList[3] = 5;

What problems do you have with that syntax?

"Sneftel is correct, if rather vulgar." --Flarelocke

##### Share on other sites
the -> is a pointer dereference operator or an indirection operator, depending on who is doing the talking. I think the second is the proper term.

##### Share on other sites
In most of the literature I''ve seen it''s called the "arrow operator".

"Sneftel is correct, if rather vulgar." --Flarelocke

##### Share on other sites
Sneftel: using ''return T();'' nor ''return NULL;'' will compile.
error when using ''return T();'' Reference initialized with ''int'', needs lvalue of type ''int'' in function LINKED_LIST::operator[](int) at line 143;

using return NULL; yeilds the same error message. :-/

##### Share on other sites
hmm, yeah. Well, really what you should do is throw an exception, since there''s nothing you could reasonably return to indicate an error. However, if you want you could do:

return *((T*)0);

"Sneftel is correct, if rather vulgar." --Flarelocke

##### Share on other sites
Sneftel:
template <typename T>T& LINKED_LIST<T>::operator[] (int subscript){    if(subscript>members)        return *((T*)0);    long start = 1;    NODE<T>*  cur_node=head;    while(start<subscript){        cur_node=cur_node->next;        start++;    }    if(cur_node)      return *cur_node->data;    else      return *((T*)0);}

now works. I dont beleive the index operator was at fault.
// fill myList, and then
myList[3] = 5;

The example now works. However, in my original code my linked_list is created by using something to the effect of LINKED_LIST* myList = new LINKED_LIST;
I do believe I will need to overload a few more operators in order to achieve the desired effect.

##### Share on other sites
If you''re declaring it as a pointer then you are SOL with respect to getting the subscript operator to work on it. You''ll just have to do (*myListPtr)[3] = 5. Why are you declaring it as a pointer, anyway?

"Sneftel is correct, if rather vulgar." --Flarelocke

##### Share on other sites
To tell you the truth. I don''t know. I''ve never been formally trained in programming. In no tutorial nor book I''ve read has the topic of relevant pointer usage been discussed. It''s always been some dumb example that just shows How to use them, not why someone would want to.
The noteable exception is passing an object to a function via pointer. Other than that, I really don''t know when to and when not.

I guess its back to the books for me.

##### Share on other sites
Are you kidding? You just showed off one of the most notable uses of pointers, namely linked lists. If there''s one thing that pointers are useful for, it''s linked lists. You''re doing pretty well so far, as far as I can tell.

"Sneftel is correct, if rather vulgar." --Flarelocke

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
632571
• Total Posts
3007127

×