先看以下两个文章:
http://www.vckbase.com/document/viewdoc/?id=689
http://www.vckbase.com/document/viewdoc/?id=412
补充:
- #include <stdlib.h> //system()
- #include <iostream> //cout<<
- #include <memory> //auto_ptr<>
- using namespace std;
- class MyClass
- {
- public:
- int x;
- MyClass()
- {
- //void constructor
- }
- ~MyClass()
- {
- //destructor
- }
- int MyFunc( int* /*const*/ y )
- {
- *y = 20; // 改变外部变量的值 int MyFunc(const int *y ) or int MyFunc( int const *y ) 时报错
- x = *y ; // 改变类内部变量值 int MyFunc( int *y ) const 时报错
- y = &x ; // 改变传来的指针参数值 int MyFunc( int *y ) const or int MyFunc( int* const y ) 时报错
- return 0;
- }
- int MyFunc2( int /*const*/ &y )
- {
- y = 20; // 改变外部变量的值 int MyFunc2(const int &y ) or int MyFunc2( int const &y ) 时报错
- x = y ; // 改变类内部变量值 int MyFunc2( int &y ) const 时报错
- return 0;
- }
- };
- //-----------------------------------------
- void Fun()
- {
- //-----------------------------------------
- MyClass m;
- int z = 10;
- m.MyFunc(&z);
- printf("call MyFunc: z=%d, MyClass.x=%d/t/n",z,m.x);
- m.MyFunc2(z);
- printf("call MyFunc2: z=%d, MyClass.x=%d/t/n",z,m.x);
- return;
- //--------------------------------------------
- }
- void main()
- {
- Fun();
- system("pause");
- }
- //-----------------------------------------