TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CAMERON CLARK
from: JERRY COFFIN
date: 1997-05-30 10:13:00
subject: template hell?

On (28 May 97) Cameron Clark wrote to All...
 CC>  dll.h - unsorted double linked list
 CC> #define dtype int
 CC> #ifndef dll_h
 CC> #define dll_h
 CC> class Node {
 CC> public:
 CC>   Node* prev;
 CC>   Node* next;
 CC>   dtype data;
 CC> };
Just FWIW, this is exactly equivalent to:
struct Node {
    Node *prev;
    Node *next;
    dtype data;
};
Many (myself included) advise using a struct when you have something
that contains only data and no functions.
 CC> class DLList {
 CC>   Node* head;                       // head node in list - dummy node
 CC> public:
 CC>   Node* cur;               // current node used to seach the list
Why do you make this public?  I can't tell how you're using it, but it
looks like something that ought to be private...
[ more code elided ]
 CC>     As you can see, dtype should be the template class. After changing
 CC> the header file and the definitions, the source compiles fine but it
 CC> says that the functions DLList::method() is not defined for
 CC> every method.
 CC>     template
 CC>     class Node {   
 CC>          Node* prev;
 CC>          Node* next;
 CC>          dtype data;
 CC>     };
 CC>     template
 CC>     class DLList { 
 CC>          Node* head;
 CC>          Node* cur;
 CC>          method(dtype item);
Again, just FWIW, unless you know you're dealing with an extremely
simple data type, you're probably better off passing a const reference
instead of an actual item by value.  I.e. I'd change this to:
              method(dtype const &item);
Okay, now for the real meat of things - the syntax for doing a templated
method, which is like this:
void DLList::method(dtype const &item) {
    // whatever
}
Basically you use:
ret_type full_class_name::method_name(params);
In the case of a template, the full name of a class looks like:
    class_name
instead of just:
    class_name
that you use with a non-templated class.  Hopefully that makes some
sense instead of just making things muddier...
    Later,
    Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.