Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

StaticList and DynamicList (7)

Shulou Source: shulou.com Published: 2022-06-03 05:04:15 09月16日 Update

As we mentioned in the previous blog section, SeqList can be derived into two subclasses, StaticList and DynamicList. So today we'll look at these two subclasses, how they're implemented and what's the difference between them.

A. StaticList design points: first of all, it must be a class template. The second is to use native arrays as sequential storage space, and the last is to use template parameters to determine array size. defined as follows

template

< typename T, int N >

class StaticList : public SeqList{protected: T m_space[N]; //Sequential storage space, N is template parameter public: StaticList(); //specify the concrete value of the parent class member int capacity() const;};

Let's go down and implement StaticList. The code is as follows

StaticList.h source code

#ifndef STATICLIST_H#define STATICLIST_H#include "Seqlist.h"namespace DTLib{template

< typename T, int N >

class StaticList : public SeqList{protected: T m_space[N]; //Sequential storage space, N is template parameter public: StaticList() //specify the concrete value of the parent class member { this->m_array = m_space; this->m_length = 0; } int capacity() const { return N; }};}#endif // STATICLIST_H

Let's write a test code to test this StaticList, main.cpp code as follows

#include #include "StaticList.h"using namespace std;using namespace DTLib;int main(){ StaticList l; for(int i=0; im_length : capacity; for(int i=0; im_array[i]; } T* temp = this->m_array; this->m_array = array; this->m_length = length; this->m_capacity = capacity; delete[] temp; } else { THROW_EXCEPTION(NoEnoughMemoryException, "No memory to resize DynamicList Object ... "); } } } ~DynamicList() { delete[] this->m_array; }};}#endif // DYNAMICLIST_H

We also write an example code to verify the DynamicList class, main.cpp code is as follows

#include #include "DynamicList.h"using namespace std;using namespace DTLib;int main(){ DynamicList l(5); for(int i=0; i

Tags: Space storage order size templates code security parameters subclasses results two functions dynamics members guarantees objects data arrays source code essentials Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Docker Shulou Tech Info Shulou Information