64,282
社区成员
发帖
与我相关
我的任务
分享
struct SRule
{
uint32_t rule_id;
SRule() : rule_id(0) {}
virtual ~SRule() {}
bool operator<(const SRule &b)
{
return rule_id < b.rule_id;
}
};
struct SRuleNumber : public SRule
{
uint32_t n;
SRuleNumber(uint32_t n) : n(n) {}
};
struct SRuleString : public SRule
{
string str;
SRuleString(string str) :str(str) {}
};
#include <iostream>
using namespace std;
struct SRule
{
uint32_t rule_id;
SRule() : rule_id(0) {}
SRule(int id) : rule_id(id) {}
virtual ~SRule() {}
bool operator<(const SRule &b)
{
return rule_id < b.rule_id;
}
};
struct SRuleNumber : public SRule
{
enum {id =1};
uint32_t n;
SRuleNumber(uint32_t n=0) :SRule(1) ,n(n) {}
};
struct SRuleString : public SRule
{
enum{id =2};
string str;
SRuleString(const string& str="") :SRule(id),str(str) {}
};
void demo(){
SRule base;
SRuleNumber n;
SRuleString s;
cout <<"base < n = "<<(base<n)<<"; base < s ="<<(base<s) <<"; n < s = "<<(n<s)<<endl;
}
int main()
{
demo();
return 0;
}
输出:
base < n = 1; base < s =1; n < s = 1
struct SRule
{
uint32_t rule_id;
SRule() : rule_id(0) {}
SRule(int id) : rule_id(id) {}
virtual ~SRule() {}
bool operator<(const SRule &b)
{
return rule_id < b.rule_id;
}
};
struct SRuleNumber : public SRule
{
enum {id =1;};
uint32_t n;
SRuleNumber(uint32_t n=0) SRule(1): n(n) {}
};
struct SRuleString : public SRule
{
enum{id =2};
string str;
SRuleString(const string& str="") :SRule(id),str(str) {}
};
SRule base;
SRuleNumber n;
SRuleString s;
cout <<"base<n = "<<(base<n)<<"base <s ="<<(base<s) <<"n<s="<<(n<s)<<endl;
你是这个意思不?
static uint32_t counter = 0;
struct SRule
{
uint32_t rule_id;
SRule() : rule_id(++counter) {}
virtual ~SRule() {}
bool operator<(const SRule &b) const
{
return rule_id < b.rule_id;
}
};
struct SRuleNumber : public SRule
{
uint32_t n;
SRuleNumber(uint32_t n) : n(n) {}
};
struct SRuleString : public SRule
{
string str;
SRuleString(string str) :str(str) {}
};
int main()
{
SRule *rule1 = new SRuleNumber(37);
SRule *rule2 = new SRuleString("z_z_q");
cout << (rule1 < rule2 ? 1 : 0) << endl;
delete rule1;
delete rule2;
return 0;
}
我在vs2015上测试,输出结果是随机的,有时候输出1,有时候输出0,晕了[/quote]
SRule *rule1 = new SRuleNumber(37);
SRule *rule2 = new SRuleString("z_z_q");
cout << (rule1 < rule2 ? 1 : 0) << endl;
这里,你比较的rule1和rule2是指针不是对象,所以有时候输出1,有时候输出0.
想比较对象的话,直接定义对象或者对指针解引用
struct SRule
{
uint32_t rule_id;
SRule() : rule_id(0) {}
virtual ~SRule() {}
virtual bool lessthan(const SRule &b)const{return rule_id < b.rule_id;}
bool operator<(const SRule &b)const
{
return lessthan(b);
}
};
struct SRuleNumber : public SRule
{
uint32_t n;
SRuleNumber(uint32_t n) : n(n) {}
bool lessthan(const SRule &b)const{return n < b.n;}
};
struct SRuleString : public SRule
{
string str;
SRuleString(string str) :str(str) {}
bool lessthan(const SRule &b)const{return str< b.str;}
};
大约这么处理
static uint32_t counter = 0;
struct SRule
{
uint32_t rule_id;
SRule() : rule_id(++counter) {}
virtual ~SRule() {}
bool operator<(const SRule &b) const
{
return rule_id < b.rule_id;
}
};
struct SRuleNumber : public SRule
{
uint32_t n;
SRuleNumber(uint32_t n) : n(n) {}
};
struct SRuleString : public SRule
{
string str;
SRuleString(string str) :str(str) {}
};
int main()
{
SRule *rule1 = new SRuleNumber(37);
SRule *rule2 = new SRuleString("z_z_q");
cout << (rule1 < rule2 ? 1 : 0) << endl;
delete rule1;
delete rule2;
return 0;
}
我在vs2015上测试,输出结果是随机的,有时候输出1,有时候输出0,晕了bool operator<(const SRule &b) const