C++高手请进,类成员函数参数默认引用类成员的困惑!!!!

followme163 2007-04-27 10:47:39
//我想在类成员函数中默认引用类成员,编译器就是通不过,代码如下
class CAbc;
class CAbc
{
private:
int m_abc;
public:
void fun0(int &p=NULL)//编译错误
{
printf("%d",p);
}

void fun1(const int &p=NULL)//编译通过
{
printf("%d",p);
}
void fun2(int &p=this->m_abc)//编译错误
{
printf("%d",p);
}

void fun3(const int &p=m_abc)//编译错误
{
printf("%d",p);
}
};
...全文
665 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2007-04-27
  • 打赏
  • 举报
回复
真是很搞笑,引用正是用指针来实现的,传指针的引用所耗空间好转指针的拷贝完全相同。
followme163 2007-04-27
  • 打赏
  • 举报
回复
为了一个指针的内存,我真是自找了这么多麻烦,还是用指针算了
//我的初期思想是这样的:
class A
{
char *s;//其它操作没有写出来,我可以保证其不为野指针
void fun(char *t=NULL)//编译通过
{
char *tem=t;//浪费了一个指针tem的内存
if (t==NULL)tem=s;
else tem=t;
}
void fun(char *&t=s)//如果用引用节省了一个指针的内存,但没有一个方法能够实现,可惜!!
{
if (t!=NULL)cout<<t<<endl;
else cout<<s<<endl;
}
};
taodm 2007-04-27
  • 打赏
  • 举报
回复
那么,啥编译错误啊?
followme163 2007-04-27
  • 打赏
  • 举报
回复
我说的是static int *m_abc;
taodm 2007-04-27
  • 打赏
  • 举报
回复
搞什么呀,int怎么可以用NULL这个void *类型的东西来初始化。
followme163 2007-04-27
  • 打赏
  • 举报
回复
但是指针却不行了,为什么?int CAbc::m_abc=NULL;//编译错误
yoyo_alex_lw 2007-04-27
  • 打赏
  • 举报
回复
不可以在普通成员函数里头使用普通的程序变量作为参数的默认值.因为这样,编译器无法确认函数的参数的默认值是多少,这个动态确定不行,只能静态确定.办法是把程序变量改成静态的,函数改成静态的
followme163 2007-04-27
  • 打赏
  • 举报
回复
以下代码通过了
class CAbc;
class CAbc
{
public:
static int m_abc;
CAbc(){}
static void set(int abc);
void fun(const int &p=m_abc)//编译错误
{
printf("%d",p);
}
};
void CAbc::set(int abc)
{
m_abc=abc;
}
int CAbc::m_abc=1000;
int main(int argc, char* argv[])
{
CAbc a;
a.set(10);
a.fun();
return 0;
}
不过以下却通不过
class CAbc;
class CAbc
{
public:
static int *m_abc;
static void set(int abc);
CAbc(){}
void fun(const int * &p=m_abc)//编译错误
{
printf("%d",*p);
}
};
void CAbc::set(int abc)
{
*m_abc=abc;
}
int CAbc::m_abc=NULL;//编译错误
int main(int argc, char* argv[])
{
CAbc a;
a.set(10);
a.fun();
return 0;
}


taodm 2007-04-27
  • 打赏
  • 举报
回复
不要用引用,用指针
followme163 2007-04-27
  • 打赏
  • 举报
回复
不知上楼的如何解决这个问题,给个example!
proxiaobai 2007-04-27
  • 打赏
  • 举报
回复
缺省参数值是静态绑定在静态类型上面的
followme163 2007-04-27
  • 打赏
  • 举报
回复
修改为以下照样出错:
const int m_abc;
void fun2(const int &p=this->m_abc)
followme163 2007-04-27
  • 打赏
  • 举报
回复
编译fun2时错误如下:
error C2355: 'this' : can only be referenced inside non-static member functions
error C2227: left of '->m_abc' must point to class/struct/union
bamboostflying 2007-04-27
  • 打赏
  • 举报
回复
只有常量引用才可以初始化
xxyifan 2007-04-27
  • 打赏
  • 举报
回复
引用必有初始值
bamboostflying 2007-04-27
  • 打赏
  • 举报
回复
没这么用过 贴错误码过来看看
followme163 2007-04-27
  • 打赏
  • 举报
回复
我的目的是想使用fun2函数.通不过呀!
bamboostflying 2007-04-27
  • 打赏
  • 举报
回复
引用怎么可以是空呢???
sunhuiyong 2007-04-27
  • 打赏
  • 举报
回复
#include <stdio.h>
class CAbc
{
public:
static int m_abc;
CAbc(){}
static void set(int abc);
void fun(const int &p=m_abc)//±àÒë´íÎó
{
printf("%d",p);
}
};
void CAbc::set(int abc)
{
m_abc=abc;
}
int CAbc::m_abc=1000;
int main(int argc, char* argv[])
{
CAbc a;
a.set(10);
a.fun();
return 0;
}
followme163 2007-04-27
  • 打赏
  • 举报
回复
解决了
#include <string>
#include <iostream.h>
using std::string;
class CAbc
{
private:
char *m_str;
public:
CAbc(){m_str=new char[128];memcpy(m_str,"原来",sizeof("原来"));}
~CAbc(){delete []m_str;}
void fun(char *t=NULL)
{
if (t!=NULL){cout<<t<<endl;return;}
else fun(m_str);
}
};
int main(int argc, char* argv[])
{
CAbc a;
a.fun("我的");
a.fun();
return 0;
}

65,212

社区成员

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

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