C + + Array class template (Stack memory)

Source: Internet
Author: User

#ifndef _array_h_

#define _array_h_

/*

* implement an array class template, on the stack

*why

*2016/9/5

*/

Template

< typename T, int N >

Class Array

{

Private

T M_array[n];

Public

int length (); Get array length

BOOL Set_array (T value, int index); Set the contents of an array element

BOOL Get_array (t& value, int index); //Get array element contents

t& operator [] (int index); //overloaded [] operator for easy operation of array objects

T operator [] (int index) const; //If the user defines a const array object, then accessing the array element requires a const-decorated member function

Virtual ~array (); //destructors are best defined as virtual functions, allowing classes that inherit this class to override this destructor. Of course this is not vitrual, because if the constructor is private rather than protected,

Just don't want this class to be inherited, so you can not vitrual, but if the constructor is protected, the explanation is to be inherited, if you want to be inherited, then the destructor is better virtual

};


Template

< typename T, int N >

int array<t, N>::length ()

{

return N;

}


Template

< typename T, int N >

BOOL Array<t, N>::set_array (T value, int index)

{

BOOL ret = (0 <= index) && (Index < N);

if (ret)

{

M_array[index] = value;

}

return ret;

}


Template

< typename T, int N >

BOOL Array<t, N>::get_array (t& value, int index)

{

BOOL ret = (0 <= index) && (Index < N);

if (ret)

{

Value = M_array[index];

}

return ret;

}


Template

< typename T, int N >

t& array<t, n>::operator[] (int index)

{

return M_array[index];

}


Template

< typename T, int N >

T array<t, n>::operator[] (int index) const

{

return M_array[index];

}



Template

< typename T, int N >

Array<t, N>::~array ()

{

}



#endif


C + + Array class template (Stack memory)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.