Too many ways. 1. Set class B as a freind class of class A, so that every B instance can directly access every member of the instance of class A. 2. Attach a class A pointer in a member function of B, such as: class B { A* m_pA = NULL; public: void Attach(const A* pA){m_pA = pA;} } 3. Associate a class A instance with a class B instance by introducing another class. Think about Document-View architecture such as CMultiDocTemplate.
========================================
如果类A要用类B的成员,那么类A必须要得到一个类B的实例,或是实例指针
========================
不能,不同的类实例有各自不同的变量数据,你最好记住,你程序中处理的是类的对象,它才是实际的,而不是处理类本身,比如int i;int是一个类,i是类的对象,你处理的是i,而不是int.
WizardK的方法不是很懂,能不能具体说一下,这种方法能保证各个类中数据的一直性吗?!
static int nSum;这样在每个实例中是一样的值。也可以在实例化之前就赋值。
====================================
第一:CString是类,但是定义它为一个成员时可以设为静态的.
第二:可以不这样做,你的对话框类写好后,你一定要弹出它,那么一定有一个地方定义了对话框类的对象吧,不要将它定义成某一个函数的局部变量,你只要将这个对话框类对象定义成全局变量就行了.因为整个程序只有这一个实例,所以没有什么一致性问题.
1. Set class B as a freind class of class A, so that every B instance can directly access every member of the instance of class A.
2. Attach a class A pointer in a member function of B, such as:
class B
{
A* m_pA = NULL;
public:
void Attach(const A* pA){m_pA = pA;}
}
3. Associate a class A instance with a class B instance by introducing another class. Think about Document-View architecture such as CMultiDocTemplate.
还有想问一下wltg2001(红猪)说的第一中可以声明为静态的,我声明后怎么是有错误
error LNK2001: unresolved external symbol "public: static class CString CParaSet::strName" (?strName@CParaSet@@2VCString@@A)
Debug/Test.exe : fatal error LNK1120: 1 unresolved externals请教一下,谢谢啦!
static CString sz;//这是写在H文件中类声明中的.CStrng 类名::sz="";//这是写在CPP文件中的.