继承关系中的小于号重载问题

z_z_q 2016-02-25 07:14:21

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) {}
};


这样的继承关系,我现在想重载小于号,由base类中的rule_id决定大小关系,不管是基类还是子类的实例,都通过rule_id比较大小,要怎么实现啊,上面那种写法没有效果,求大神解答该怎么实现!
...全文
204 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2016-02-26
  • 打赏
  • 举报
回复
这样定义 operator 的话,子类不需要重载 operator 只需要给每个子类 一个不同的ID,并在初始化的时候,初始化子类继承而来的id ,为子类本身的ID
begodliker 2016-02-26
  • 打赏
  • 举报
回复
引用 10 楼 lm_whales 的回复:

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;
你是这个意思不?
lm_whales 2016-02-26
  • 打赏
  • 举报
回复
#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
lm_whales 2016-02-26
  • 打赏
  • 举报
回复

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;
你是这个意思不?
z_z_q 2016-02-26
  • 打赏
  • 举报
回复
引用 6 楼 lm_whales 的回复:

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;}
};
大约这么处理
和要求的不太一样,不管是基类还是子类的实例,都通过rule_id比较大小,而不是比较各自的数据成员
begodliker 2016-02-26
  • 打赏
  • 举报
回复
引用 2 楼 z_z_q 的回复:
[quote=引用 1 楼 akirya 的回复:] bool operator<(const SRule &b) 后面加上 const
bool operator<(const SRule &b) const
const只是限制了不能修改数据成员吧,试了下还是不行。

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. 想比较对象的话,直接定义对象或者对指针解引用
z_z_q 2016-02-26
  • 打赏
  • 举报
回复
引用 4 楼 begodliker 的回复:
n(n)、str(str),你这么写,你是在给局部变量赋值,你的成员变量还是未定义的
不是哦,这么写能正确给成员变量赋值,亲测可行
lm_whales 2016-02-26
  • 打赏
  • 举报
回复

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;}
};
大约这么处理
z_z_q 2016-02-26
  • 打赏
  • 举报
回复
引用 3 楼 begodliker 的回复:
楼主,你是在比较指针吗?
我希望是可以用指针比较两个对象,有什么办法可以做到么
begodliker 2016-02-26
  • 打赏
  • 举报
回复
n(n)、str(str),你这么写,你是在给局部变量赋值,你的成员变量还是未定义的
begodliker 2016-02-26
  • 打赏
  • 举报
回复
楼主,你是在比较指针吗?
z_z_q 2016-02-26
  • 打赏
  • 举报
回复
引用 1 楼 akirya 的回复:
bool operator<(const SRule &b) 后面加上 const
bool operator<(const SRule &b) const
const只是限制了不能修改数据成员吧,试了下还是不行。

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
bool operator<(const SRule &b) const

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧