Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
namespace CW_Core { template<class T> class TArray; } template<class T> class CW_Core::TArray { public: TArray(int nQuantity); // TArray(const TArray& rkObject); // ~TArray(); // TArray<T>& operator=(const TArray<T>& rkObject); T& operator[](int i); const T& operator[](int i) const; T* GetArray(); const T* GetArray() const; void Append(const T& rtElement); void SetElement(int i, const T& rtElement); void Remove(int i); void RemoveAll(); void SetMaxQuantity(int nNewMaxQuantity); int GetMaxQuantity() const; private: int m_nMaxQuantity; T* m_atArray; }; template<class T> CW_Core::TArray<T>::TArray(int nQuantity = 1) { if(nQuantity<1) nQuantity = 1; m_nMaxQuantity = nQuantity; m_atArray = 0; SetMaxQuantity(m_nMaxQuantity); } template<class T> CW_Core::TArray<T>::TArray(const TArray& tObject) { m_atArray = 0; *this = tObject; } template<class T> CW_Core::TArray<T>::~TArray() { free(m_atArray); } template<class T> inline CW_Core::TArray<T>& CW_Core::TArray<T>::operator=(const TArray<T>& rkObject) { m_nMaxQuantity = rkObject.m_nMaxQuantity; if(m_atArray) free(m_atArray); if(m_nMaxQuantity > 0) { m_atArray = (T*)malloc(sizeof(T)*m_nMaxQuantity); for (int i = 0; i < m_nMaxQuantity; i++) m_atArray[i] = rkObject.m_atArray[i]; } else { m_atArray = 0; } return *this; } template<class T> inline T& CW_Core::TArray<T>::operator[](int i) { if(i>m_nMaxQuantity-1) i = m_nMaxQuantity-1; return m_atArray[i]; } template<class T> inline const T& CW_Core::TArray<T>::operator[](int i) const { if(i>m_nMaxQuantity-1) i = m_nMaxQuantity-1; return m_atArray[i]; } template<class T> inline T* CW_Core::TArray<T>::GetArray() { return m_atArray; } template<class T> inline const T* CW_Core::TArray<T>::GetArray() const { return m_atArray; } template<class T> inline void CW_Core::TArray<T>::Append(const T& rtElement) { SetMaxQuantity(m_nMaxQuantity+1); m_atArray[m_nMaxQuantity-1] = rtElement; } template<class T> inline void CW_Core::TArray<T>::SetElement(int i, const T& rtElement) { if(i>=m_nMaxQuantity) Append(rtElement); else { SetMaxQuantity(m_nMaxQuantity+1); for(int j=m_nMaxQuantity-1; j>i; j--) m_atArray[j] = m_atArray[j-1]; m_atArray[i] = rtElement; } } template<class T> inline void CW_Core::TArray<T>::Remove(int i) { for(int j=m_nMaxQuantity-1; j>i; j--) { m_atArray[j-1] = m_atArray[j]; } if(i<m_nMaxQuantity) SetMaxQuantity(m_nMaxQuantity-1); } template<class T> inline void CW_Core::TArray<T>::RemoveAll() { SetMaxQuantity(0); } template<class T> inline void CW_Core::TArray<T>::SetMaxQuantity(int nNewMaxQuantity) { if(nNewMaxQuantity) { if(!m_atArray) m_atArray = (T*)malloc(sizeof(T)*nNewMaxQuantity); else { m_atArray = (T*)realloc(m_atArray, sizeof(T)*nNewMaxQuantity); m_nMaxQuantity = nNewMaxQuantity; } } else { free(m_atArray); m_atArray = 0; m_nMaxQuantity = 0; } } template<class T> inline int CW_Core::TArray<T>::GetMaxQuantity() const { return m_nMaxQuantity; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <iostream> #include <cstdio> #include <strstream> #include "CW_TArray.h" using namespace std; int main() { CW_Core::TArray<int> a(5); for(int i=0; i<a.GetMaxQuantity(); i++) a[i]=i; a.SetMaxQuantity(3); a.Append(6); a.Append(7); a.Append(55); a.SetElement(1, 4); FILE* s = fopen("kai.ntz", "wb"); fwrite((void*)&a, sizeof(CW_Core::TArray<int>)*a.GetMaxQuantity(), 1, s); fclose(s); s = fopen("kai.ntz", "rb"); cout<<a.GetMaxQuantity()<<endl; CW_Core::TArray<int> b(7); cout<<a.GetMaxQuantity()<<endl; fread((void*)&b, sizeof(CW_Core::TArray<int>)*a.GetMaxQuantity(), 1, s); cout<<a.GetMaxQuantity()<<endl; for(i=0; i<b.GetMaxQuantity(); i++) cout<<b[i]<<endl; fclose(s); return 0; } |
Zitat
7
7
3284056 <-- hier der Fehler WIESO???? :crying:
0
4
1
2
6
7
55
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 |
fread((void*)&b, sizeof(CW_Core::TArray<int>)*a.GetMaxQuantity(), 1, s); |
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// ///////////////////////////////////////////////// // GLOBAL // // Funktion zum Vergrößern eines Speicherbereiches // z. B. ein Array. // // [in] array : Das 'Quellarray'. // [in] old_size: Alte größe. // [in] new_size: Neue größe. // // [ret] Das neue Array. // ///////////////////////////////////////////////// template<typename T> inline T *resize (T *array, unsigned long old_size, unsigned long new_size) { // Speicher reservieren für das neue Array. T *temp = new T[new_size]; // Den Inhalt von 'array' in 'temp' kopieren. ::memcpy (temp, array, old_size); // Altes Array löschen. delete [] array; // Neues zurück geben. return (temp); } |
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
namespace CW_Core { template<class T> class TArray; } template<class T> class CW_Core::TArray { public: TArray(int nQuantity); // TArray(const TArray& rkObject); // ~TArray(); // TArray<T>& operator=(const TArray<T>& rkObject); T& operator[](int i); const T& operator[](int i) const; T* GetArray(); const T* GetArray() const; void Append(const T& rtElement); void SetElement(int i, const T& rtElement); void Remove(int i); void RemoveAll(); void SetMaxQuantity(int nNewMaxQuantity); int GetMaxQuantity() const; private: int m_nMaxQuantity; T* m_atArray; }; template<class T> CW_Core::TArray<T>::TArray(int nQuantity = 1) { if(nQuantity<1) nQuantity = 1; m_nMaxQuantity = nQuantity; m_atArray = 0; SetMaxQuantity(m_nMaxQuantity); } template<class T> CW_Core::TArray<T>::TArray(const TArray& tObject) { m_atArray = 0; *this = tObject; } template<class T> CW_Core::TArray<T>::~TArray() { delete[] m_atArray; } template<class T> inline CW_Core::TArray<T>& CW_Core::TArray<T>::operator=(const TArray<T>& rkObject) { m_nMaxQuantity = rkObject.m_nMaxQuantity; if(m_atArray) delete[] m_atArray; if(m_nMaxQuantity > 0) { m_atArray = new T[m_nMaxQuantity]; for (int i = 0; i < m_nMaxQuantity; i++) m_atArray[i] = rkObject.m_atArray[i]; } else { m_atArray = 0; } return *this; } template<class T> inline T& CW_Core::TArray<T>::operator[](int i) { if(i>m_nMaxQuantity-1) i = m_nMaxQuantity-1; return m_atArray[i]; } template<class T> inline const T& CW_Core::TArray<T>::operator[](int i) const { if(i>m_nMaxQuantity-1) i = m_nMaxQuantity-1; return m_atArray[i]; } template<class T> inline T* CW_Core::TArray<T>::GetArray() { return m_atArray; } template<class T> inline const T* CW_Core::TArray<T>::GetArray() const { return m_atArray; } template<class T> inline void CW_Core::TArray<T>::Append(const T& rtElement) { SetMaxQuantity(m_nMaxQuantity+1); m_atArray[m_nMaxQuantity-1] = rtElement; } template<class T> inline void CW_Core::TArray<T>::SetElement(int i, const T& rtElement) { if(i>=m_nMaxQuantity) Append(rtElement); else { SetMaxQuantity(m_nMaxQuantity+1); for(int j=m_nMaxQuantity-1; j>i; j--) m_atArray[j] = m_atArray[j-1]; m_atArray[i] = rtElement; } } template<class T> inline void CW_Core::TArray<T>::Remove(int i) { for(int j=m_nMaxQuantity-1; j>i; j--) { m_atArray[j-1] = m_atArray[j]; } if(i<m_nMaxQuantity) SetMaxQuantity(m_nMaxQuantity-1); } template<class T> inline void CW_Core::TArray<T>::RemoveAll() { SetMaxQuantity(0); } template<class T> inline void CW_Core::TArray<T>::SetMaxQuantity(int nNewMaxQuantity) { if(nNewMaxQuantity) { if(!m_atArray) m_atArray = new T[nNewMaxQuantity]; else { T* tmp = new T[m_nMaxQuantity]; memcpy((void*)tmp, (const void*)m_atArray, sizeof(T)*m_nMaxQuantity); delete[] m_atArray; m_atArray = new T[nNewMaxQuantity]; memcpy((void*)m_atArray, (const void*)tmp, sizeof(T)*m_nMaxQuantity); m_nMaxQuantity = nNewMaxQuantity; delete[] tmp; } } else { delete[] m_atArray; m_atArray = 0; m_nMaxQuantity = 0; } } template<class T> inline int CW_Core::TArray<T>::GetMaxQuantity() const { return this->m_nMaxQuantity; } |
Werbeanzeige