在帮我看看const的使用吧

Zhentiwei 2007-08-12 11:23:13

#include <algorithm>
#include <vector>

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

class HE_edge
{
public:
inline bool operator == ( const HE_edge &ea);
inline HE_edge *next() const;
// 这个地方为什么不能用const啊?
inline void setnext(//const? HE_edge *next);
inline void seta( const int a);
inline int a() const;
private:
HE_edge *next_;

int a_;
};
//这个地方啊>
inline void HE_edge::setnext(//const HE_edge *next)
{
this->next_ = next;
}
inline void HE_edge::seta( const int a)
{
this->a_ = a;
}
inline bool HE_edge::operator ==(const HE_edge &ea)
{
return ( this->next_ == ea.next() && this->a_ == ea.a() );

}
inline HE_edge *HE_edge::next() const
{
return this->next_;
}
inline int HE_edge::a() const
{
return a_;
}
int main()
{
HE_edge value;
HE_edge *edgel = new HE_edge;
HE_edge ia[10];
for(int i = 0; i < 10 ; ++i)
{
if( i % 2 == 0)
{
ia[i].seta(0);
ia[i].setnext(NULL);
}
else
{
ia[i].seta(i);
ia[i].setnext(edgel);
}
}
value.seta(0);
value.setnext(NULL);
vector<HE_edge> vec( ia, ia + 10 );
vector<HE_edge>::iterator vec_iter;
vec_iter = remove( vec.begin(), vec.end(), value);
vec.erase( vec_iter, vec.end() );
vec_iter = vec.begin();
int m = 0;
for(; vec_iter != vec.end(); ++vec_iter )
{
m ++;
cout<<vec_iter->a()<<endl;
}
return 0;
}
那个地方怎么才用const呢?
用了回有下面的错误:
error C2440: '=' : cannot convert from 'const class HE_edge *' to 'class HE_edge *'
Conversion loses qualifiers
...全文
207 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
emptyness 2007-08-12
  • 打赏
  • 举报
回复
函数体内代码造成的: this->next_ = next;
具体原因..think下..
Zhentiwei 2007-08-12
  • 打赏
  • 举报
回复
谢谢你的回复。
结帖了。
emptyness 2007-08-12
  • 打赏
  • 举报
回复
可以~
...随你..呵呵


恩..是的.
你上次那个帖子我看了.结果是库文件里出错了.
不写还没不清楚为什么不可以.. 翻了些书上 发现重载函数 只要应该加const全加了
const符号, 但是没看到哪里有那么一句说参数必须是const类型的..
也许是vector自己定义的是const 使用这个模版的时候要遵循吧.
我没怎么使用过stl标准模版库..
Zhentiwei 2007-08-12
  • 打赏
  • 举报
回复
另外,问你,可以加你为我的好友吗?
Zhentiwei 2007-08-12
  • 打赏
  • 举报
回复
恩,是不是 "==" 重载要写const吧?
我上次不写,在用vector的时候就不行,是不是这样的啊?
emptyness 2007-08-12
  • 打赏
  • 举报
回复
另外 对于const参数 如果不是非内部数据结构的引用类型就没必要使用const..
一般都是 类 或者结构, 数组 传递的时候才用 const type &
其他情况下const参数 基本没出场的时候..
emptyness 2007-08-12
  • 打赏
  • 举报
回复
类型要求很不严格

多打了个" 很 " ,其实C也是严格的,但对于const不是很严格..
emptyness 2007-08-12
  • 打赏
  • 举报
回复
晕, 比如说:
const int i; //i是const;
const int * a=i; //指向i,
如果能有int * p=a;
的话.
我直接 (*a) ++就改变了i的内容.
所以C++要求 const的类型指针只能赋值给const类型指针.不能给一般的指针...

有的版本的C也实现了const ,在C的环境下, 类型要求很不严格,可能会通过编译..
但C++下是肯定不行的.C++对类型要求十分严格.
长尾巴的悟空 2007-08-12
  • 打赏
  • 举报
回复
限制常量嘛
Zhentiwei 2007-08-12
  • 打赏
  • 举报
回复
恩,照你说的,我改了,可以的。
你能解释一下吗?
为什么要这样?
而我的
int a_;就可以呢?
const HE_edge *next_;
在使用他的时候没有什么限制吗?
非常谢谢!
emptyness 2007-08-12
  • 打赏
  • 举报
回复
HE_edge *next_;

OK 问题在于这里..

你这个指针没有定义成const类型的..所以不能把const类型的指针给你..
不然你就可以通过这个指针改变原来本是const的值...

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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