奇怪的现象,大家说说是怎么回事

chengyuny 2002-04-12 03:52:36
我在一个类里定义了一个指针型私有成员变量
private:
Point3d _pivot;
int _last_lod_level;
Expression _CurrentExpression;
// UINT nCurMuscleIndex;
(pCurrentMuscle定义如果放在这,程序就出错:非法内存访问)
bool _showMuscle;
int _lod_level;
GLenum _appearance;
Muscle * _current_moving_muscle;
Vector<Vector3d> _displacement;
Vector<Pointer<Muscle> > _vec_p_muscles;
Eyes _right_eye;
Eyes _left_eye;
Teeth _upper_teeth;
Teeth _lower_teeth;
Tongue _tongue;
Skin _skin;
int nb_ver_influenced;
Muscle* pCurrentMuscle;//?????????????????????????
double m_xEyeRotate;
double m_yEyeRotate;
当我把pCurrentMuscle放在上面所示的位置时,程序则运行正常
大家说说这是怎么回事呀??
...全文
34 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LionEagle 2002-04-12
  • 打赏
  • 举报
回复
光从声明很难看出问题的,信息太少了
winfit 2002-04-12
  • 打赏
  • 举报
回复
有可能对其他变量赋值的时候,把该pCurrentMuscle覆盖掉了,导致调用pCurrentMuscle的时候出错,譬如

private:
char str[10];
Muscle* pCurrentMuscle;
};
char temp[256] = "this string will exced the limit of string str";
strcpy(*.str, temp);

chengyuny 2002-04-12
  • 打赏
  • 举报
回复
可是pCurrentMuscle跟别的成员变量没有联系,因此也就无所谓初始话先后问题
,还可能有其他原因吗?
loveyly 2002-04-12
  • 打赏
  • 举报
回复
i can not see the realy mean.please spesk more widely.thanks.
LionEagle 2002-04-12
  • 打赏
  • 举报
回复
Effective C++条款13:initializaion list中的members初始化次序应该和其
在class内的声明次序相同

如class A
{
int m_size;
int m_low;
int m_high;
public:
A( int low,int high )
: m_low(low),
m_high(high),
m_size( m_high-m_low+1 ) // m_size的值不是high-low+1,
// 因为m_size最先初始化,而此时
// m_high和m_low尚未初始化
// 这和写成赋值的情况是不一样的
{ ... }
}

写成赋值的情况是这样的:

A::A( int low,int high )
{
m_low = low;
m_high = high;
m_size = m_high - m_low + 1; // here, m_size==high - low + 1
}
prototype 2002-04-12
  • 打赏
  • 举报
回复
probably due to your initialization order of your private data members.
AdvRom 2002-04-12
  • 打赏
  • 举报
回复
pCurrentMuscle不初始化就用,不非法内存访问才怪

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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