Home arrow Linix - How to arrow Introduction to Templates

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
Introduction to Templates PDF Print E-mail
Written by Hemanshu Patel   
Wednesday, 24 October 2007
Article Index
Introduction to Templates
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
 


Holding objects by value

Actually creating a copy of the objects inside a generic container is a complex problem if you don&rsquot have templates. With templates, things are relatively simple &ndash you just say that you are holding objects rather than pointers:

//: C16:ValueStack.h 

>// Holding objects by value in a Stack

>#ifndef VALUESTACK_H


>#define VALUESTACK_H


>#include "../require.h"

>


>template<class T, int ssize = 100>


>class Stack {


> // Default constructor performs object

> // initialization for each element in array:

> T stack[ssize];


> int top;


>public:


> Stack() : top(0) {}


> // Copy-constructor copies object into array:

> void push(const T& x) {


> require(top < ssize, "Too many push()es");


> stack[top++] = x;


> }


> T peek() const { return stack[top]; }


> // Object still exists when you pop it;

> // it just isn't available anymore:

> T pop() {


> require(top > 0, "Too many pop()s");


> return stack[--top];


> }


>};


>#endif // VALUESTACK_H ///:~

The copy constructor for the contained objects does most of the work by passing and returning the objects by value. Inside push( ), storage of the object onto the Stack array is accomplished with T::operator=. To guarantee that it works, a class called SelfCounter keeps track of object creations and copy-constructions:

//: C16:SelfCounter.h 

>#ifndef SELFCOUNTER_H


>#define SELFCOUNTER_H


>#include "ValueStack.h"

>#include <iostream>


>


>class SelfCounter {


> static int counter;


> int id;


>public:


> SelfCounter() : id(counter++) {


> std::cout << "Created: " << id << std::endl;


> }


> SelfCounter(const SelfCounter& rv) : id(rv.id){


> std::cout << "Copied: " << id << std::endl;


> }


> SelfCounter operator=(const SelfCounter& rv) {


> std::cout << "Assigned " << rv.id << " to "

> << id << std::endl;


> return *this;


> }


> ~SelfCounter() {


> std::cout << "Destroyed: "<< id << std::endl;


> }


> friend std::ostream& operator<<(


> std::ostream& os, const SelfCounter& sc){


> return os << "SelfCounter: " << sc.id;


> }


>};


>#endif // SELFCOUNTER_H ///:~

>


>//: C16:SelfCounter.cpp {O}

>#include "SelfCounter.h"

>int SelfCounter::counter = 0; ///:~

>


>//: C16:ValueStackTest.cpp

>//{L} SelfCounter

>#include "ValueStack.h"

>#include "SelfCounter.h"

>#include <iostream>


>using namespace std;


>


>int main() {


> Stack<SelfCounter> sc;


> for(int i = 0; i < 10; i++)


> sc.push(SelfCounter());


> // OK to peek(), result is a temporary:

> cout << sc.peek() << endl;


> for(int k = 0; k < 10; k++)


> cout << sc.pop() << endl;


>} ///:~

When a Stack container is created, the default constructor of the contained object is called for each object in the array. You&rsquoll initially see 100 SelfCounter objects created for no apparent reason, but this is just the array initialization. This can be a bit expensive, but there&rsquos no way around it in a simple design like this. An even more complex situation arises if you make the Stack more general by allowing the size to grow dynamically, because in the implementation shown above this would involve creating a new (larger) array, copying the old array to the new, and destroying the old array (this is, in fact, what the Standard C++ Library vector class does).



Last Updated ( Wednesday, 24 October 2007 )
 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits