PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : static pointer array in einer klasse



barton4
01-09-2008, 17:38
Hallo, hab hier ein Klasse names CSortAlgorithm. In der sind verschiede Sortiertalgorithmen als statische funktion implementiert sind. Nun möchte ich gerne diese statsischen Funktionen über ein Functionspointerarray ansprechen. Jeder Algorithmus bekommt einen Index und die Funktion soll dann über dieses Pointer Array aufgerufen werden.

Angesprochen werden dann die Funktionen von "Außen" über
"static bool (*pAlgo[count_of_algorithm])(T element[], t_length length);"


hier mal ein bischen Code:

CSortalgorithm.h


template <class T> class CSortAlgorithm
{
public:
CSortAlgorithm();
~CSortAlgorithm();

//some behaviour paramers
static bool withBenchmark;
static int maxThreads;

//numeric index for sort algorithm
static const t_algo_index indexSelectionSort = 0;
static const t_algo_index indexInsertSort = 1;
static const t_algo_index indexBubbleSort = 2;
static const t_algo_index indexQuickSort = 3;
static const t_algo_index indexQuickSortT = 4;
static const t_algo_index indexMergeSort = 5;
static const t_algo_index indexCppStdSort = 6;
static const t_algo_index indexMergeSortVFast = 7;
static const t_algo_index indexBinTreeSort = 8;
static const t_algo_index count_of_algorithm = 9;

//array of pointers
static bool (*pAlgo[count_of_algorithm])(T element[], t_length length);
static string getName(t_algo_index index);


//additional functions
static void swap(T& a, T& b);

//sortalgorithm
static bool SelectionSort(T elements[], t_length length);
static bool InsertSort(T elements[], t_length length);
static bool BubbleSort(T elements[], t_length length);
static bool QuickSort(T elements[], t_length length);
static bool QuickSortT(T elements[], t_length length);
static bool MergeSort(T elements[], t_length length);
static bool MergeSortVFast(T elements[], t_length length);
static bool CppStdSort(T elements[], t_length length);
static bool SortTest(T elements[], t_length length);
static bool BinTreeSort(T elements[], t_length length);

static bool QuickSort(T elements[], t_length length, bool withThreads); //deprecated
private:
#ifdef POSIX
static void *QuickSortPthread(void* attr);
#endif
static bool QuickSort(T elements[], t_pos left, t_pos right);
static bool QuickSortWithThreads(T elements[], t_pos left, t_pos right, s_manageThread* threadMgmt);
};


CSortAlgorithm.cpp



template <class T> bool CSortAlgorithm<T>::withBenchmark = true;
template <class T> int CSortAlgorithm<T>::maxThreads = 100;


template class CSortAlgorithm<int>;

bool CSortAlgorithm<int>::*pAlgo[CSortAlgorithm<int>::count_of_algorithm] =
{
&CSortAlgorithm<int>::SelectionSort;
&CSortAlgorithm<int>::InsertSort;
&CSortAlgorithm<int>::BubbleSort;
&CSortAlgorithm<int>::BinTreeSort;
&CSortAlgorithm<int>::QuickSort;
&CSortAlgorithm<int>::QuickSortT;
&CSortAlgorithm<int>::MergeSort;
&CSortAlgorithm<int>::MergeSortVFast;
&CSortAlgorithm<int>::CppStdSort;
};


g++ output


./src/sortalgorithm/CSortAlgorithm.cpp:480: error: expected `}' before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:480: error: cannot convert ‘bool (*)(int*, unsigned int)’ to ‘bool CSortAlgorithm<int>::*’ in initialization
./src/sortalgorithm/CSortAlgorithm.cpp:481: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:482: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:483: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:484: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:485: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:486: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:487: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:488: error: expected constructor, destructor, or type conversion before ‘;’ token
./src/sortalgorithm/CSortAlgorithm.cpp:489: error: expected declaration before ‘}’ token
make: *** ["build/"CSortAlgorithm.o] Error 1


Irgendwie klappt das nicht mit dem zuweisen der Adressen an die Funktionspointer.
Das es noch eine Template Klasse ist macht die ganze Sache irgendwie noch kniffeliger.

Kann mir jemand weiter helfen

Gruss Martin

locus vivendi
01-09-2008, 21:23
Erstens werden in der Initialisierung des Arrays die Elemente durch Kommas getrennt, nicht durch Semikolons.

Zweitens deckt sich bei der Initialisierung der Elementtyp nicht mit dem der Deklaration. Es müssen auch bei der Initialisierung Funktionszeiger sein.

Drittens musst du auch bei der Initialisierung des Arrays angeben, dass es sich in einem Template befindet (bei "withBenchmark" und "maxThreads" hast du es auch gemacht).

RHBaum
02-09-2008, 13:33
Ausserdem versteh ich ned, wieso er funktionspointer braucht.

templates = c++
funktionspointer = c
Technologie fuer unterschiedliches laufzeitverhalten bei gleicher syntax unter c++ = virtuelle funktionen ...

ciao ...

barton4
04-09-2008, 14:27
hi, danke erstmal für die Hilfe, es geht nun alles...

An Virtuelle Funktionen hab ich noch gar nicht gedacht, es ist aber auch so das ich am Anfang nicht vor hatte das über indizes anzusprechen und da haben Funktionspointerarrays ganz gut gepasst.

falls es noch jemand interessiert, so schaut es nun aus und es geht einwandfrei :D:



//create instantiations witch an constructor
template <class T> bool CSortAlgorithm<T>::withBenchmark = true;
template <class T> int CSortAlgorithm<T>::maxThreads = 8;

template <class T> bool (*CSortAlgorithm<T>::pAlgo[CSortAlgorithm<T>::count_of_algorithm])(T element[], t_length length) =
{
&CSortAlgorithm<T>::SelectionSort,
&CSortAlgorithm<T>::InsertSort,
&CSortAlgorithm<T>::BubbleSort,
&CSortAlgorithm<T>::QuickSort,
&CSortAlgorithm<T>::QuickSortT,
&CSortAlgorithm<T>::MergeSort,
&CSortAlgorithm<T>::CppStdSort,
&CSortAlgorithm<T>::MergeSortVFast,
&CSortAlgorithm<T>::BinTreeSort
};

template class CSortAlgorithm<char>;
template class CSortAlgorithm<short>;
template class CSortAlgorithm<int>;
template class CSortAlgorithm<long long>;