Trong bài này em dùng TryAlgorithm để thể hiện cho thuật toán cụ thể tìm kiếm, nó là một subclass của BetterAlgorithm.
Nhưng khi run thì operation makeItBetter của TryAlgorithm không được thực thi mà nó lại tìm đến BetterAlgorithm::makeItBetter.
Em không hiểu tại sao, có ai giúp em với. Em xin cảm ơn.
SearchInAll.h
Code:
#pragma once
#include "stdafx.h"
#include <iostream>

//-----------------------------------------------------------------------------
/*template <class T> class AbstractArray{
public:
	AbstractArray(){};
	AbstractArray(T*,int);
	int sizeOfArray;
	T*a;
};*/
template <class T> class ConcreteArray/*:public AbstractArray<T>*/{
public:
	T*a;
	int sizeOfArray;
	ConcreteArray(T*,int);
	ConcreteArray(){};
};

//-----------------------------------------------------------------

template <class T> class BetterAlgorithm{
public:
	BetterAlgorithm(){};
	virtual void makeItBetter(T*,int&,int(*)(T*))=0;
};

template <class T> class BetterArray:public ConcreteArray<T>{
public:
	BetterArray<T>(){};
	BetterArray(ConcreteArray<T>*,BetterAlgorithm<T>*);
	BetterAlgorithm<T>* betterAlgorithm;
	ConcreteArray* arrayToSearch;
	T*a;
	int sizeOfArray;
	void getResult();
};


//------------------------------------------------------------------------------

template <class T> class BestAlgorithm{
public: 
	virtual void makeItBest(T*,int*)=0;
};

template <class T> class BestArray:public ConcreteArray<T>{
public:
	BestArray(ConcreteArray<T>*,BestAlgorithm<T>*);
	ConcreteArray* arrayToSearch;
	BestAlgorithm* bestAlgorithm;
	virtual int isBest(T,T);
	T*a;
	int sizeOfArray;
};




//--------------------------------------------------------------------

/*template <class T> AbstractArray<T>::AbstractArray(T* b,int n){
	a=b;
	sizeOfArray=n;
}*/
template <class T> ConcreteArray<T>::ConcreteArray(T * b,int n){
	a=b;
	sizeOfArray=n;
}

template <class T> BetterArray<T>::BetterArray(ConcreteArray<T> *m, BetterAlgorithm<T> *n){
	arrayToSearch=m;
	betertAlgorithm=n;
	a=arrayToSearch->a;
	sizeOfArray=arrayToSearch->sizeOfArray;
}

template <class T> BestArray<T>::BestArray(ConcreteArray<T> *m, BestAlgorithm<T> *n){
	arrayToSearch=m;
	bestAlgorithm=n;
	a=arrayToSearch->a;
	sizeOfArray=arrayToSearch->sizeOfArray;
}
/*template <class T> void BetterArray<T>::getResult(){
	betterAlgorithm->makeItBetter(a,sizeOfArray,isBetter);
}*/
SearchInAll.cpp
Code:
// SearchInArray.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "SearchInArray.h"
using namespace std;

template <class T> class TryAlgorithm:public BetterAlgorithm<T>{
public:
	TryAlgorithm(){};
	virtual void makeItBetter(T* x ,int& n,int(*isBetter)(T*)) override;
};
template <class T> void TryAlgorithm<T>::makeItBetter(T * x,int& n,int (* isReallyBetter)(T*)){
	int i=0;
	T y[1000];
	T*z=x;
	for (int j=0;j<n;j++){
		if ((*isReallyBetter)(z)){
			z++;
			y[i++]=*z;
		}
	}
	x=y;
	n=i;
}
//-----------------------------------------------------------------------------

template <class T> class OddArray:public BetterArray<T>{
public:
	ConcreteArray* arrayToSearch;
	BetterAlgorithm<T>* betterAlgorithm;
	OddArray(ConcreteArray<T>* m,BetterAlgorithm<T>* n);
	T*a;
	int sizeOfArray;
	static int isBetter(T*);
	void getResult();
};
template <class T> int OddArray<T>::isBetter(T* m){
	return (*m%2==0);
}
template <class T> void OddArray<T>::getResult(){
	betterAlgorithm->makeItBetter(a,sizeOfArray,isBetter);
}
template <class T> OddArray<T>::OddArray(ConcreteArray<T> *m, BetterAlgorithm<T> *n){
	arrayToSearch=m;
	betterAlgorithm=n;
	a=m->a;
	sizeOfArray=m->sizeOfArray;
}
//-----------------------------------------------------------------------------
int main(){
	int x[10]={1,3,5,7,2,4,6,8,0,9};
	ConcreteArray<int>* m1=new ConcreteArray<int>();
	TryAlgorithm<int>* m2=new TryAlgorithm<int>();
	OddArray<int>* xx=new OddArray<int>(m1,m2);
	xx->getResult();

}