本例是B类的的成员变量“m_getvariate” 得到A类的对象a的成员变量“m_betransed”的值,对值进行修改后,再把值赋给m_betransed。
1.B类构建公共函数——void getvalue(const int variate ) { m_getvariate = variate;},这个函数用来把A类的成员变量的值传递给B;
2.B类中的void changeValue( )函数来对变量的值进行修改;
3.B类构建公共函数——const int setValue( ){ return m_getvalue} 把值返回给B类。
示例代码:
#include <iostream>
using namespace std;
class B;
class A
{
public:
A( void );
void f_init();
void prinvariate();
private:
int m_betransed;
};
class B
{
public:
void getvariate( int t_variate );
void changeValue();
int setvariate();
private:
int m_getvariate;
};
int main()
{
A *a;
a= new A;
a->prinvariate( );
return 0;
}
A::A()
{
f_init();
}
void A::f_init()
{
m_betransed = 2;
B * b ;
b = new B;
b->getvariate( m_betransed );//b->m_getvariate = 2
b->changeValue( );//b->m_getvariate = 3
m_betransed = b->setvariate();//A->m_betransed = 3
}
void B::getvariate(int t_variate )
{
m_getvariate = t_variate;
}
void A::prinvariate()
{
cout << m_betransed;
}
void B::changeValue()
{
++m_getvariate;
}
int B::setvariate()
{
return m_getvariate;
}