====== C++ Crashkurs / Übung 3 ====== Die Listenimplementierung wird in eine Klasse "CListe" überführt. Dadurch entfällt die Initialisierung und Freigabe der inneren Struktur der Klasse. Diese Aufgabe übernehmen Constructor und Destructor. Das schützt gegen vergessene Initialisierung und Freigebe des Speichers, hindert jedoch die fehlerhafte Datenmanipulation nicht. **Ueb2b.h:** // File : Ueb2b.h // #include "..\stdafx.h" #include "Ueb2b.VerketteteListe.h" using namespace Ueb2b; int _ueb (int argc, _TCHAR* argv[]) { CListe* pListe = new CListe(); //InitialisiereListe(pListe); // Nicht mehr nötig - die Initialisierung geschieht im Constructor pListe->ElementAnhaengen(4711); pListe->ElementAnhaengen(815); pListe->ElementAnhaengen(42); std::cout << "Liste 1:" << std::endl; for (LISTEN_ELEMENT* iter = pListe->pStart; iter != NULL; iter = iter->pNaechster) { std::cout << " " << iter->nInhalt << std::endl; } //free(pListe); // free() wird duch delete ersetzt, so dass der Destructor der Klasse aufgerufen wird delete pListe; pListe = NULL; // Böse 3: Listenelemente werden manipuliert CListe* pListe4 = new CListe(); //InitialisiereListe(pListe4); pListe4->ElementAnhaengen(4711); pListe4->ElementAnhaengen(815); pListe4->ElementAnhaengen(42); std::cout << "Liste 4 manipuliert:" << std::endl; for (LISTEN_ELEMENT* iter = pListe4->pStart; iter != NULL; iter = iter->pNaechster) { iter->nInhalt += 9999; iter->pNaechster = pListe4->pStart; std::cout << " " << iter->nInhalt << std::endl; } delete pListe4; pListe4 = NULL; return 0; } // EOF **Ueb2b.VerketteteListe.h:** // File : Ueb2b.VerketteteListe.h // namespace Ueb2b { struct LISTEN_ELEMENT { int nInhalt; LISTEN_ELEMENT* pNaechster; }; class CListe { public: LISTEN_ELEMENT* pStart; public: CListe () { pStart = NULL; } ~CListe () { ElementFreigeben(pStart); } void ElementAnhaengen (int nInhalt) { LISTEN_ELEMENT* pNeu = (LISTEN_ELEMENT*) malloc(sizeof(LISTEN_ELEMENT)); pNeu->pNaechster = NULL; pNeu->nInhalt = nInhalt; if (pStart == NULL) { pStart = pNeu; } else { LISTEN_ELEMENT* pLetztes = pStart; while (pLetztes->pNaechster != NULL) pLetztes = pLetztes->pNaechster; pLetztes->pNaechster = pNeu; } } private: void ElementFreigeben (LISTEN_ELEMENT* pElement) { if (pElement->pNaechster != NULL) ElementFreigeben(pElement->pNaechster); free(pElement); pElement = NULL; } }; } // EOF **Programm.cpp:** - Der Einstiegspunkt // Programm.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Ueb2b.h" int _tmain (int argc, _TCHAR* argv[]) { return _ueb(argc, argv); } // EOF **stdafx.h:** // stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once #include "targetver.h" #include #include // TODO: reference additional headers your program requires here #include #include // EOF **targetver.h:** // Including SDKDDKVer.h defines the highest available Windows platform. // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. #pragma once #include ---- Stand: 19.09.2014 --- //[[feedback.jk-wiki@kreick.de|: Jürgen Kreick]]// EOF