Need help with Templates, Linked Lists

Discussion about everything. New games, 3d math, development tips...
Post Reply
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Need help with Templates, Linked Lists

Post by twilight17 »

I read the whole Teach yourself C++ in 21 Days book by Jesse Liberty, and understood most of it... except for Templates, and Linked Lists. I just don't get it...

such as

template <class T> (is this always supposed to be t?)

Also, this:


Code: Select all

template <class T>
Array<T>::Array(int size) : itsSize = size //I don't understand this
{
    pType = new T[size] // I really dont get this
    for (int i=0; i<size; i++) //I understand this
    pType[i] = 0; //all good
}

I just need an explanation (or more) for all of this

Linked lists also (too big to post example)

Can someone explain why these are important?

and does anyone know of a good tut for these? Or just explain them... Thank you.
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

ok, first off
template <class T> and template <typename T> are THE EXACT SAME THING! Use whichever you like. Some like to philosophy that one or the other represent this or that concept better, but whatever, no convention exist and they are exactly the same code-wise.

second
template <class T> and template <class Blergh> are THE EXACT SAME THING IF you replace all T occurences in your function/class by Blergh.

third, there are 2 types of templates. class templates and function templates. For this I suggest a bit of internet reading as their can be a lot of subtelties there. In general, and this is far from being an absolute in any case, you'll use more often function templates than class templates.

Linked list can be one or two sided. Basically, each list node keeps it's data and a pointer to the next element in the list (and possibly the previous one, if it's two-sided). This allow scattering the nodes through the memory as well as some nice tricks, though if you just read that book, you're probably not at that level yet (don't despair, if you keep hard at it, you'll be there before you know it and you might even create tricks of your own ;) )

Hope that helps :)

-EDIT-
I did not see the whole of your post. It seems you lack other understanding too. So let's try those ^^

In a CONSTRUCTOR and ONLY in those, you can pass arguments in a special way. a constructor is the function in the class that has no return arguments and is named exactly the same name as the class (the destructor, in opposition, is named ~classname)

Before the body of the function (the {} part), and after the signature (what comes before, aka the return type, function name, arguments and modifiers like const), you can write : . This means you will initialize some members with something their instead of leaving them uninitialized/initialized with default values. If your class contains an int named i (how original ^^), you could choose to initialize it with 3 for example. It would look something like that:

ClassA::ClassA(blah A, int c) : i(3) {}

You can initialize with an argument too, like this:

ClassA::ClassA(blah A, int c) : i(c) {}

to initialize more than one, you simply separate them with commas:

ClassA::ClassA(blah A, int c) : blahFromA(A), i(c) {}

There's plenty of advantages to this technique and no real disadvantages. You should learn to use it as much as possible ^^

Also, in case you don't know about the ::

if you give the body of a function WITHIN a class, you do not need to tell it's parent's name, like so:

class A
{
public:
void Test() {} //Here you don't need to tell it it's an A class, it already knows.
};

but

class A
{
public:
void Test();
};

A::Test() {} // Here you do since you are outside the body of the class ^^

Hope that'll help :)
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post by twilight17 »

Wow! Thanks for the in depth explanation!

That helped a ton!

I knew about the constructors and the :: "scope resolution operator" but thanks for explaining so well!

:mrgreen:
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post by twilight17 »

Oh: and I still don't get a little bit of that code :?

Code: Select all


Array<T>::Array(int size) : itsSize = size
this is what I don't get. Does this make an array, with the size specified like this Array<10>, then does it access it self?... after that it initializes size which is what is passed as the array size? Is this correct, Thank you :)
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

No.
<T> is the type, as in int, float, char, customClassA
(int size) is the size of the array.
So creating it you'd most likelly go:

Array<int> Numbers(10);
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post by twilight17 »

Dorth wrote:No.
<T> is the type, as in int, float, char, customClassA
(int size) is the size of the array.
So creating it you'd most likelly go:

Array<int> Numbers(10);
Ohhh soo Array (the class) <int> (the type of array you want) Numbers (name of the array) (10); (Size of the array!)

I understand now! Thank you a ton.

:D
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
Post Reply