下面的类能被泛型算法支持么?
比如:list<Log> lLog;
Log.h内容如下:
#pragma once
// CLogclass CLog : public CWnd
{
DECLARE_DYNAMIC(CLog)public:
CLog();
CLog(int i,CString s);
virtual ~CLog();
bool operator==( CLog log );
bool operator<( CLog log );
protected:
DECLARE_MESSAGE_MAP()
private:
int iId;
CString sBody;
};
Log.cpp内容如下:
// Log.cpp : 实现文件
//#include "stdafx.h"
#include "smschat.h"
#include "Log.h"
#include <list>
// CLogIMPLEMENT_DYNAMIC(CLog, CWnd)
CLog::CLog()
{
iId = 0;
}CLog::CLog(int i,CString s)
{
this->iId = i;
this->sBody = s;
}CLog::~CLog()
{
}bool CLog::operator == ( CLog log )
{
if ( this->iId == log.iId && this->sBody == log.sBody )
{
return true;
}
else
{
return false;
}
}bool CLog::operator < ( CLog log )
{
if ( this->iId < log.iId )
{
return true;
}
else
{
return false;
}
}BEGIN_MESSAGE_MAP(CLog, CWnd)
END_MESSAGE_MAP()// CLog 消息处理程序
比如:list<Log> lLog;
Log.h内容如下:
#pragma once
// CLogclass CLog : public CWnd
{
DECLARE_DYNAMIC(CLog)public:
CLog();
CLog(int i,CString s);
virtual ~CLog();
bool operator==( CLog log );
bool operator<( CLog log );
protected:
DECLARE_MESSAGE_MAP()
private:
int iId;
CString sBody;
};
Log.cpp内容如下:
// Log.cpp : 实现文件
//#include "stdafx.h"
#include "smschat.h"
#include "Log.h"
#include <list>
// CLogIMPLEMENT_DYNAMIC(CLog, CWnd)
CLog::CLog()
{
iId = 0;
}CLog::CLog(int i,CString s)
{
this->iId = i;
this->sBody = s;
}CLog::~CLog()
{
}bool CLog::operator == ( CLog log )
{
if ( this->iId == log.iId && this->sBody == log.sBody )
{
return true;
}
else
{
return false;
}
}bool CLog::operator < ( CLog log )
{
if ( this->iId < log.iId )
{
return true;
}
else
{
return false;
}
}BEGIN_MESSAGE_MAP(CLog, CWnd)
END_MESSAGE_MAP()// CLog 消息处理程序
DECLARE_MESSAGE_MAP()->你的Message Mapping为何要声明成protected,我觉得一般都认是public的。
CLog(int i,CString s);
bool operator==( CLog log );
bool operator<( CLog log );
这三个函数。我要用list。我看c++primer上说要满足3条(有默认构造函数,支持==和<)才能用list
list<x>中的x可以是任何类名
第一次设计类,第一次用list,可能问题比较可笑,各位大虾多包涵。能帮我看看那个运算符重载对不对么?
是不是bool CLog::operator == (const CLog &log )这样定义比较好啊?