C++类之间相互调用
全局变量实现类间调用
假设工程里有A类、B类两个类,相应的文件有A.h、B.h、main.cpp、A.cpp、B.cpp。
1、在main.cpp中主函数前利用extern声明全局变量( extern A* g_pA; extern B* g_pB; ),并定义变量( A* g_pA=nullptr; B* g_pB=nullptr; ),在主函数中设置g_pA与g_pB分别指向的实例对象空间地址;
2、在A.cpp中包含头文件“B.h”,并声明 extern B* g_pB后,这样就可以在A类中调用B类的公共函数;
3、同理,在B.cpp中包含头文件“A.h”,并声明 extern A* g_pA后,这样就可以在B类中调用A类的公共函数。
声明:这里举例没有什么实际意义可能不太合理,只是为了便于理解全局变量实现类间调用。
// main.cpp
#include "A.h"
#include "B.h"
extern A* g_pA;
extern B* g_pB;
A* g_pA=nullptr;
B* g_pB=nullptr;
int main()
{
g_pB= new B(111);
g_pA= new A(-1);
bool tmpCalFlag = g_pB->cal();
bool tmpCompareFlag = g_pA->compare();
return 0;
}
//A.h
#ifndef _A_H_
#define _A_H_
class A
{
pivate:
int m_iValue;
public:
A(int _value=0);
~A();
bool compare();
void setValue(const int& _value){m_iValue=_value;}
const int getValue() const {return m_iValue;}
};
#endif _A_H_
// A.cpp
#include "A.h"
#include "B.h"
extern B* g_pB;
A::A(int _value=0):m_iValue(_value)
{
}
A::~A()
{
}
bool A::compare()
{
if(g_pB && (g_pB->getNumber() > 100))
{
return true;
}
else
{
return false;
}
}
//B.h
#ifndef _B_H_
#define _B_H_
class B
{
pivate:
int m_iNumber;
public:
B(int _number=0);
~B();
bool cal();
void setNumber(const int& _number){m_iNumber=_number;}
const int getNumber() const {return m_iNumber;}
};
#endif _B_H_
// B.cpp
#include "A.h"
#include "B.h"
extern A* g_pA;
B::B(int _number=0):m_iNumber(_number)
{
}
B::~B()
{
}
bool B::cal()
{
if(g_pA && (g_pA->getValue() < 0))
{
return false;
}
else
{
return true;
}
}
优缺点
优点:
简单快速便于理解,每个类只有一个实例对象,实例个数少时使用方便,推荐使用此方法。
缺点:
类个数多,每个类要创建多个实例对象,使用容易出错,不建议使用此方法。
类内指针实现类间调用
1、在头文件“A.h”,class A之前前置声明class B,并class A中声明成员指针B* m_pB,在“A.cpp”开头引入“B.h”;
2、在头文件“B.h”,class B之前前置声明class A,并class B中声明成员指针A* m_pA,在“B.cpp”开头引入“A.h”;
3、在main.cpp主函数中分别创建A类B类的实例对象,并分别对其成员指针进行赋值。这样就阔以互相调用彼此的公共成员函数了。
声明:这里举例没有什么实际意义可能不太合理,只是为了便于理解全局变量实现类间调用
// main.cpp
#include "A.h"
#include "B.h"
int main()
{
B* tmpB= new B(111);
A* tmpA= new A(-1);
tmpB->setA(tmpA);
tmpA->setB(tmpB);
bool tmpCalFlag = tmpB->cal();
bool tmpCompareFlag = tmpA->compare();
return 0;
//A.h
#ifndef _A_H_
#define _A_H_
class B;
class A
{
pivate:
int m_iValue;
B* m_pB;
public:
A(int _value=0);
~A();
bool compare();
void setValue(const int& _value){m_iValue=_value;}
const int getValue() const {return m_iValue;}
void setB(B* _pB){ m_pB = _pB; }
B* getB() const { return m_pB; }
};
#endif _A_H_
// A.cpp
#include "A.h"
#include "B.h"
A::A(int _value=0):m_iValue(_value)
{
m_pB=nullptr;
}
A::~A()
{
}
bool A::compare()
{
if(m_pB && (m_pB->getNumber() > 100))
{
return true;
}
else
{
return false;
}
}
//B.h
#ifndef _B_H_
#define _B_H_
class A;
class B
{
pivate:
int m_iNumber;
A* m_pA;
public:
B(int _number=0);
~B();
bool cal();
void setNumber(const int& _number){m_iNumber=_number;}
const int getNumber() const {return m_iNumber;}
void setA(A* _pA){ m_pA = _pA; }
A* getA() const { return m_pA; }
};
#endif _B_H_
// B.cpp
#include "A.h"
#include "B.h"
B::B(int _number=0):m_iNumber(_number)
{
m_pA=nullptr;
}
B::~B()
{
}
bool B::cal()
{
if(m_pA && (m_pA->getValue() < 0))
{
return false;
}
else
{
return true;
}
}
优缺点
优点:
类个数多,每个类要创建多个实例对象快速方便,建议使用此方法。
缺点:
相对复杂,初次使用理解困难。