原来的程序相对复杂, 本文简化说明。


       到处是坑啊:

#include<iostream>
using namespace std;

class A
{
public:
	int x;
	A()
	{
		x = 0;
	}

	void test();
};


void A::test()
{
	int x = 100;
}



int main(void)
{
	A a;
	a.test();

	cout << a.x << endl;

    return 0;
}
      a.x的值居然是0, 不是100,  在仔细看了下, 尼玛, 局部变量和成员变量同名了, 这是程序风格的大忌啊, 修改如下:

#include<iostream>
using namespace std;

class A
{
public:
	int m_x;
	A()
	{
		m_x = 0;
	}

	void test();
};


void A::test()
{
	m_x = 100;
}



int main(void)
{
	A a;
	a.test();

	cout << a.m_x << endl;

    return 0;
}
      这下好了。 真操蛋, 程序风格又给我上了一课。




 


本文转载:CSDN博客