现在的位置: 首页 > 综合 > 正文

this作参数的问题

2013年08月04日 ⁄ 综合 ⁄ 共 349字 ⁄ 字号 评论关闭

今天写程序遇到一个挺有意思的compile error

vs2005中的c2662;

 

class test
{
public:
void shout(){cout<<"shout"<<endl;}
}


main()
{
   
const test* t = new test;
   t
->shout();
}

就会出错,问题在于shout函数有this作为参数,但是我用的是const指针调用shout;

那么指针类型就无法匹配;shout加个const修饰就好了

 

class test
{
public:
void shout()const{cout<<"shout"<<endl;}
}


main()
{
   
const test* t = new test;
   t
->shout();
}

 

 

抱歉!评论已关闭.