-
March 19th, 2009, 03:35 PM
#16
Re: general purpose Singly-linked list implementation with inheritance
It seems to me, that Iterator is exactly the thing I need. So I have to give up with template implementation.
Two more questions, probably the last ones:
1) Is that ok, to allow access to data of the list only via Iterator?
2) Should I allow to inherit my slist?
Last edited by andrey_zh; March 20th, 2009 at 04:41 AM.
-
March 19th, 2009, 06:13 PM
#17
Re: general purpose Singly-linked list implementation with inheritance
Originally Posted by andrey_zh
It seems to me, that Iterator is the exactly thing I need. So I have to give up with template implementation.
You don't have to "give up" anything. How does the std::list accomplish this? An std::list uses iterators to access the elements.
Code:
#include <list>
#include <iostream>
int main()
{
std::list<int> IntList;
//...
IntList.push_back(10);
IntList.push_back(20);
IntList.push_back(30);
std::list<int>::iterator it = IntList.begin();
std::list<int>::iterator itend = IntList.end();
while (it != itend)
std::cout << *it << "\n";
//
}
Everything you've said about templates is proven to be wrong:
1) You don't need all of that indirection to access an element.
2) Iterators can be implemented and used with a templated class.
So you need to answer a basic question -- how does std::list seem to accomplish everything that you say you want to do?
The only conclusion that anyone reasonable can come with as to why you can't implement templated list class with iterators is that you do not currently have the knowledge to write the templated class correctly to do these things. The code above proves that there is nothing stopping you from writing the singly-linked list as a templated code using iterators to access the elements.
There is nothing wrong with not knowing how to do something, all of us has asked questions and learned. However, what is wrong is stating conclusions based on faulty "facts".
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 19th, 2009 at 06:29 PM.
-
March 19th, 2009, 06:20 PM
#18
Re: general purpose Singly-linked list implementation with inheritance
Originally Posted by andrey_zh
But how about "black box" concept? I want something simple outside and not seen complexity level inside.
The std::list is a black box. You give it a T, and by the magic of templated code, you have a linked list of T. You don't have to change any code in the std::list to support your T.
This is why templates are used. With the code you have now, you have to fool around with the internals of your list class just to support a different type. That is not user-friendly or simple.
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|