怎么在一个类中访问main函数里的变量呢
怎样在一个类中访问main函数里的变量呢?
在mycls的成员函数中怎样能够操作到abc呢?
------解决思路----------------------
CMyClass 的构造函数接受一个 int 的指针,然后把 abc 的地址传进去
------解决思路----------------------
用引用:
#include<iostream>
using std::cout;
using std::endl;
class people
{
private:
int num;
public:
people(int n):num(n){}
void Swap(int &m){m=num;}
};
int main()
{
int mum=9;
cout<<mum<<endl;
people p1(10);
p1.Swap(mum);
cout<<mum<<endl;
return 0;
}
运行结果:
9
10
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
在mycls的成员函数中怎样能够操作到abc呢?
main()
{
int abc;
CMyClass mycls;
.......
}
------解决思路----------------------
CMyClass 的构造函数接受一个 int 的指针,然后把 abc 的地址传进去
------解决思路----------------------
用引用:
#include<iostream>
using std::cout;
using std::endl;
class people
{
private:
int num;
public:
people(int n):num(n){}
void Swap(int &m){m=num;}
};
int main()
{
int mum=9;
cout<<mum<<endl;
people p1(10);
p1.Swap(mum);
cout<<mum<<endl;
return 0;
}
运行结果:
9
10
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.