class B;
class C;
再定义 class B/C 实体
 class C中有个变量是  B* classB,
这样可以在C中用 classB->ShareVar;

解决方案 »

  1.   

    #include <iostream.h>
    class A{
    public:
    A() {}
    };class B:public A{
    public:
    B(int i):A() {a=i;} protected:   //派生类的成员函数可以访问,但派生类的对象不能访问
    //public:    //派生类的成员函数可以访问,派生类的对象也能访问
    int a;
    };class C:public B{
    public:
    C(int i):B(i) {}
    int read() {return a;}
    };void main(){
    C c(2);
    cout<<"a="<<c.read()<<endl;
             //cout<<"a="<<c.a<<endl;   //上面设为public时可以使用
    }
      

  2.   

    据我对C++的理解,好像是不行的,除非C能得到B的指针。还有一种方法是在A类中设一static变量,这样B和C就可以通过该变量通信
      

  3.   

    hi , I can not fully understand you, could you make more detailed?if a->b and a->c , and you can not access the member variable if you don't create the object of b first.
      

  4.   

    感谢sxbyl、ClarkF两位兄台,我读过一遍C++之后,想法和你们一样。但如果我在C中创建B的实例b,再使用b的公有成员变量好像就可以了。才疏学浅,敬待指正。
      

  5.   

    "可以访问"和"可以取得当前某一实例的成员变量值"不是同一问题.
    你和sxbyl和ClarkF的方法都可以实现对成员变量的访问(在编译时可以通过),但是,不能取得实例类的成员变量,例如:
    解决办法只能是通过上层应用来传递.