PostHeaderIcon With Tempalte to Create Array

I’m sorry,long time have no updates, Jan is Chinese Spring Festival,I just go back home and stay with my family.

This month back company and a lot of things to do. several web applications are under developed,but not in C++,

just in Java and PHP.Actually, c++ is my favorite snack.

In your daily C++ programs, array is a common container.maybe you tell me you like vector instead of array,

Ok, good action, enjoy vector in C++.But sometime you have to use array, I do not want a detailed statement

about it.

Let’s begin, it’s so easy. you maybe write better template array than it .

#include <cassert>
template<typename array_type,
	unsigned int size> class array {
		static const unsigned int ARRAY_SIZE = size;
		array_type data[ARRAY_SIZE];
		array(void) {};
		array(const array& other_array) {
			memcopy(data,, sizeof(data));
		~array() {};
		array& operator = (const array& other_array) {
			memcopy(data,, sizeof(data));
			return (*this);
		array_type& operator[](int index) {
			assert(index >= 0);
			assert(index < ARRAY_SIZE);
			return (&data[index]);

Hide the code inside a template, You don’t have to add asserts every time you access an array.

the work is done for you automatically, it includes assert statements which prevent you from

overflowing the array.

But it doesn’t provide a way of converting an array into a pointer.

If you need it,do it yourself.

It is up to you whether or not you consider this a simple way or a bug.


Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by