MFC에서 C++로 응용 프로그램을 작성했습니다. 데이터베이스에서로드 된 모든 데이터를 저장할 수있는 클래스를 작성해야합니다.이 데이터에는 int, string, byte, boolean, datetime 등과 같은 모든 종류의 데이터 유형이 포함될 수 있습니다. 우리는 이러한 데이터를 필터링하거나 열을 교환하거나 정렬 할 수 있습니다. 예를 들면 :C++ 클래스를 설계하는 방법은 무엇입니까?
int int string bool double float .... string
0 1 "a" false 3.14 3.0 "b"
1 2 "5" true 3.22 4 "c"
참고 : 우리는 우리가 우리의 고려 사항이 있기 때문에, 정렬하거나 필터링하려면 SQL을 사용하지 않았다.
우리는 다음과 같은 클래스를 작성했습니다. 누군가가 더 좋은 제안을 할 수 있으니 사용하기 위해 샘플 클래스를 작성해주십시오. 미리 감사드립니다!
#ifndef __LIST_DATA_MODEL_H__
#define __LIST_DATA_MODEL_H__
#include <vector>
using std::vector;
///implement a pure virtual base class; parameters of function is a void pointer,
class FieldType
{
public:
enum {
TypeChar = 0,
TypeString = 1,
TypeBool = 2,
TypeShort = 3,
TypeUShort = 4,
TypeInt = 5,
TypeUInt = 6,
TypeLong = 7,
TypeULong = 8,
TypeLongLong = 9,
TypeULongLong = 10,
TypeFloat = 11,
TypeDouble = 12
};
};
template <typename _ValueType, typename _SyncType>
class Column
{
protected:
CString m_szFieldName;
vector<_ValueType> m_vValues;
public:
Column();
Column(CString szFieldName);
Column(const Column& other);
virtual ~Column();
public:
virtual BOOL LoadData(...);
public:
///This function will call LoadData function to re-load data,
///if subclass this class, please implement your LoadData function
///if you want additional operation when load data.
CALLBACK BOOL Update();
public:
const int ValueCount() const;
const CString& FieldName() const;
const _ValueType& ValueAt(int iPos) const;
///Before you call LoadData function or Update Function, the values will not updated;
void SetFieldName(const CString& szFieldName);
void SetValue(const _ValueType& val, int iPos);
};
template<class _Type>
class DataItem
{
protected:
_Type _value;
public:
DataItem();
DataItem(const DataItem& other)
{
_value = other._value;
};
DataItem(const _Type& val)
{
_value = val;
};
virtual ~DataItem()
{
};
public:
const _Type& GetValue()
{
return _value;
};
void SetValue(const _Type& value)
{
_value = value;
};
void ResetValue()
{
_value = _Type();
};
public:
bool operator ==(DataItem& right)
{
return _value == right._value;
};
bool operator <(const DataItem& right)
{
return _value < right._value;
};
const DataItem& operator =(const DataItem& right)
{
if(this == &right)
return *this;
_value = right._value;
return *this;
};
virtual DataItem* Clone()
{
return new DataItem(*this);
};
};
typedef DataItem<int> IntItem;
typedef DataItem<float> FloatItem;
typedef DataItem<double> DoubleItem;
typedef DataItem<CString> StringItem;
typedef DataItem<bool> BoolItem;
typedef DataItem<TCHAR> CharItem;
typedef DataItem<char> ByteItem;
typedef DataItem<CString> CStringItem;
#endif
_V는 컴파일러 및 표준 라이브러리의 예약 된 접두사이므로 _ValueType은 잘못된 이름입니다. _A - _Z에도 동일하게 적용되므로 정리해야 할 몇 가지 이름이 있습니다. 모든 경우에 제거 할 수 있습니다. 접두어로 붙인 모든 이름은 이미 범위가 지정되어 있으며 전역 이름은 없습니다. – MSalters
구조에 대한 조언이 있으십니까? – MemoryLeak
"고려 사항"으로 인해 SQL을 사용하여 정렬 또는 필터링하지 않은 원인은 무엇입니까? 데이터베이스가 인덱스를 이용할 수 있고 전체 테이블을 클라이언트로 전송하는 것을 피할 수 있기 때문에 SQL에서 정렬 또는 필터링이 더 빠를 것입니다. –