Skip to main content
GameDev.net gamedev.net
🔒 Locked

Templates?

Started by lone_ranger Jul 25, 2002 at 6:31 AM 7 replies 250+ views
Original Post
lone_ranger
lone_ranger
I have the c++ book "C++ the complete reference 2nd edition" 1990 it refers to template as a keyword "reserved for future use" What is a template?
Virtus junxit, mors non separabit
davepermen
davepermen
a simple thing. and it has tons of features and huge power. and is loved and hated like that. the simple idea is to template parameters. like this:


  
template<typename SomeType>
class Twin {
SomeType first_twin;
SomeType second_twin;
};

class Object;//some object


void main() {
Twin<int> int_twin; //generate a twin of ints

int_twin.first_twin is an int, and you can use it as int;
int_twin.second_twin is an int as well..;
Twin<Object> obj_twin; //generate a twin of objects

obj_twin.first_twin is here an 'Object';
obj_twin.second_twin as well..;
}


the power of this?
at first: general containers like some dynamic arrays..


  
#include <vector>
...
vector<Object> object_array; //creates a dynamic sizeable array of Objects

Object x; //creates an Object

object_array.push_back(x); //adds the Object to the array



hope you get the idea.. the power of this, you'll learn yourself. if you need...

"take a look around" - limp bizkit
www.google.com

[edited by - davepermen on July 25, 2002 7:54:49 AM]
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
lone_ranger
lone_ranger
Thanks davepermen!
Virtus junxit, mors non separabit
Shadowdancer
Shadowdancer
(nitpicking on)
Of course there''s the "pair" template in the STL that does pretty much what you wrote as the first example.
(nitpicking off)
davepermen
davepermen
[nit]a pair does not need to be a twin. two married persons are a pair as well, but they are not twins..
[/nit]

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Arild Fines
Arild Fines
quote:
Original post by lone_ranger
I have the c++ book "C++ the complete reference 2nd edition" 1990


You really, really need a newer book if you are serious about learning C++. Anything published earlier than 1999 should be considered suspect.




"The churches used to win their arguments against atheism, agnosticism, and other burning issues by burning the ismists, which is fine proof that there is a devil but hardly evidence that there is a God."
Ben Lindsey and Wainwright Evans
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Shannon Barber
Shannon Barber
quote:
Original post by Arild Fines
You really, really need a newer book if you are serious about learning C++. Anything published earlier than 1999 should be considered suspect.



I was going to say the same thing, the language was still significantly evolving until mid 1995, settled down a bit, and was finally standardized in late 1998.

(And Microsoft still hasn't implemented a reasonably compliant compiler, nearly four years later.)

[edited by - Magmai Kai Holmlor on July 25, 2002 8:52:49 PM]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Beer Hunter
Beer Hunter
quote:
Original post by davepermen
template
class Twin {
 SomeType first_twin;
 SomeType second_twin;
};

Those ought to be public

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.