Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

StaticList and DynamicList (7)

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report