I'm trying to create a list class in C++ similar to the lists in Java. Is there a way I can have it be able to list whatever object it wants to? The class resizes arrays to create the list, but what I need to do is find out the kind of object that's needed to store.
            Asked
            
        
        
            Active
            
        
            Viewed 134 times
        
    0
            
            
        - 
                    9[C++ templates](http://www.cplusplus.com/doc/tutorial/templates/)? – tangrs Jul 12 '13 at 02:02
- 
                    3Templates can certainly do the job (though comparing templates to Java generics is a bit like comparing a Formula 1 race car to a child's pedal car with a number painted on the side). – Jerry Coffin Jul 12 '13 at 02:05
- 
                    2[not writing templates in c++ is like watching porn without mastrubating.](http://chat.stackoverflow.com/transcript/10?m=10483030#10483030), felt this quote was relevant. – Rapptz Jul 12 '13 at 02:05
- 
                    1Standard template library or templates, depends on your need (I can't really see it from the question). – darxsys Jul 12 '13 at 02:05
- 
                    Isn't the generic feature in Java learned from C++ template? – Yu Hao Jul 12 '13 at 02:06
- 
                    @YuHao: I think both came from Ada generics; Java's generics seem a lot closer to Ada 83 generics than to C++ templates. – Jerry Coffin Jul 12 '13 at 02:08
- 
                    2[Templates vs. generics](http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B#Templates_vs._generics) – Yu Hao Jul 12 '13 at 02:10
- 
                    @YuHao I liked "Templates are Turing-complete" from that link. – brian beuning Jul 12 '13 at 03:02
- 
                    I want to point out that C++'s "generics" are among the best. You can do a whole lot more than what you're asking here. – chris Jul 12 '13 at 03:19
- 
                    They are completely different things, despite the syntax. C++ templates create new type;, generate new code; and don't really fit properly into the type system. Java Generics constrain existing types; don't generate any code at all; and are *part of* the type system. You cannot compare them in any meaningful way. – user207421 Jul 14 '13 at 00:46
2 Answers
8
            
            
        Yes, C++ has templates that can be used to create generic containers roughly similar to Java generic containers.
While your immediate reaction might be to assume that std::list is similar to a Java list, that would be a mistake. In Java, a list basically just means a sequence. In C++, a std::list is a linked list (which is rarely useful). Most of the time you want to use an std::vector (which is more like Java's ArrayList).
 
    
    
        Jerry Coffin
        
- 476,176
- 80
- 629
- 1,111
2
            
            
        Yes, there is, and it's called Templates
 
    
    
        Renato Lochetti
        
- 4,558
- 3
- 32
- 49
- 
                    5
- 
                    
- 
                    3I am sure your C++ book covers that. You could have a look at it first. – R. Martinho Fernandes Jul 12 '13 at 02:10
- 
                    @MonetR.Adams you get a c++ book or a tutorial and learn about them. – Borgleader Jul 12 '13 at 02:10
- 
                    @MonetR.Adams: If you don't have a good `C++` book look here: [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/445976) – Blastfurnace Jul 12 '13 at 02:15
