通过c ++中的函数访问另一个类变量

时间:2014-09-07 03:14:55

标签: c++ class variables

我正在尝试更改类A的变量的值,通过在类B中定义/声明的函数。我尝试了多种方法,但我没有找到一个正在工作的方法。

File : myheader.h

class A {
public:
int x;
};


class B:public A
{
public:
void Print(void)
  {
    A::x=0;
  }
};

好的,在主...

#include <iostream>
#include "myheader.h"

using namespace std;

A mya;
B myb;

int main(){
mya.x=10;
cout<<mya.x<<endl; //That will print 10
myb.Print();
cout<<mya.x<<endl; //That will print also 10, i want it to print 0
return 0;
}

打印10和10.我希望通过B类中的函数访问A类中的变量x。我找不到有效的东西......

1 个答案:

答案 0 :(得分:3)

在您的代码中,myamyb是单独的实例。两个完全不同的对象。

myb.Print()更改x对象(myb)上myb.x的值,但对mya不执行任何操作。尝试使用上一个myb.x行打印cout

cout << myb.x << endl;

------编辑(回应你的评论)------

请考虑以下代码。

B的实例(在本例中为Cat)对A的实例(在本例中为Animal)进行操作的方法是传递引用到A的实例到B的实例(tom.eats( jerry );)。

在此示例中,实例为tomjerryspike。与您的myamyb类似。

class Animal {

public:

    Animal() { _alive = true; }

    bool isAlive() { return _alive; }                   // getter
    void setAlive( bool value ) { _alive = value; }     // setter

    bool _alive;

};

class Cat : public Animal {

public:

    void eats( Animal &animal ) {
        animal.setAlive( false );
    }

};

class Mouse : public Animal { };


int main( int argc, char **argv ) {
    Cat             tom;
    Mouse           jerry;
    Animal          spike;              // a dog, but could be any animal

    cout << tom.isAlive() << endl;      // true, the cat named "tom" is alive
    cout << jerry.isAlive() << endl;    // true, the mouse named "jerry" is alive
    cout << spike.isAlive() << endl;    // true, the dog named "spike" is alive

    tom.eats( jerry );                  // the cat eats the mouse.  so sad.
                                        // the cat can do this because Mouse is also an Animal

    cout << tom.isAlive() << endl;      // true, the cat named "tom" is alive
    cout << jerry.isAlive() << endl;    // false, the mouse named "jerry" is not alive
    cout << spike.isAlive() << endl;    // true, the dog named "spike" is alive

    tom.eats( spike );                  // the cat eats the dog. impressive.
                                        // the cat can do this because spike is an Animal

    cout << tom.isAlive() << endl;      // true, the cat named "tom" is alive
    cout << jerry.isAlive() << endl;    // false, the mouse named "jerry" is not alive
    cout << spike.isAlive() << endl;    // false, the dog named "spike" is not alive

    tom.eats( tom );                    // the cat eats itself.  this cat is really hungry. 
                                        // the cat can do this because Cat is an Animal

    cout << tom.isAlive() << endl;      // false, the cat named "tom" is not alive
    cout << jerry.isAlive() << endl;    // false, the mouse named "jerry" is not alive
    cout << spike.isAlive() << endl;    // false, the dog named "spike" is not alive

    spike.eats( tom );                  // error, Animal class has no eats() method so
                                        // this won't compile
}