简体   繁体   English

如何从其他类访问不同类中的变量?

[英]How to access variables in different class from other class?

Let just say that we have two classes, A and B . 我们假设我们有两个类, AB
Here is code for both of them 这是他们两个的代码

class A
{
public:
    int x;
};

class B
{
public:
    int y;
    void FindY() { y = x + 12; }
};

void something()
{
    A fs;
    B fd;
    fs.x = 10;
    fd.FindY();
}

the problem is that i want to access x but i don't wanna pass anything as argument to my function i look at friend and inheritance but both didn't seem to help me, correct me if i'm wrong. 问题是,我想访问x,但我不想传递任何东西作为我的功能的参数我看看朋友和继承,但两者似乎没有帮助我,纠正我,如果我错了。
some how i need to find x in function FindY() . 一些我如何在函数FindY()找到x。
I'm going with the static method but in my case i get this error. 我要使用静态方法,但在我的情况下,我得到这个错误。

Error 2 error LNK2001: unresolved external symbol "public: static class std::vector<class GUIDialog *,class std::allocator<class GUIDialog *> > Window::SubMenu" (?SubMenu@Window@@2V?$vector@PAVGUIDialog@@V?$allocator@PAVGUIDialog@@@std@@@std@@A) C:\\Users\\Owner\\documents\\visual studio 2010\\Projects\\Monopoly\\Monopoly\\Window.obj Here is how i declared it 错误2错误LNK2001:未解析的外部符号“public:static class std::vector<class GUIDialog *,class std::allocator<class GUIDialog *> > Window::SubMenu" (?SubMenu@Window@@2V?$vector@PAVGUIDialog@@V?$allocator@PAVGUIDialog@@@std@@@std@@A) C:\\Users\\Owner\\documents\\visual studio 2010\\Projects\\Monopoly\\Monopoly\\Window.obj以下是我如何宣布它

static vector<GUIDialog *> SubMenu;

I get that error because of this line 由于这条线,我得到了那个错误

SubMenu.resize(3);

Three different approaches: 三种不同的方法:

  1. Make B::FindY take an A object as a parameter 使B :: FindY将A对象作为参数

     class B { public: void FindY(const A &a) { y = ax + 12; } }; 
  2. Make A::x static 使A :: x静态

     class A { public: static int x; }; class B { public: void FindY() { y = A::x + 12; } }; 
  3. Make B inherit from A. 使B继承自A.

     class B : public A { public: void FindY() { y = x + 12; } }; 

CashCow also points out more ways to do this in his answer. CashCow还在他的回答中指出了更多的方法。

As it is, x is not a variable of the class A but a variable of objects ("instances") of class A . 因为它是, x不是类的变量A但对象的类的变量(“实例”) A There are at least two ways to make x accessible from B::findY without passing anything to the function: 至少有两种方法可以从B::findY访问x而不向函数传递任何内容:

  • Instantiate an object of class A inside the B::findY function: B::findY函数中实例化A类的对象:
class B
    {
    public:
        int y;
        void FindY() { A a; y = a.x + 12; }
    };
  • Make x a static variable, so that it's a variable on the class itself. 使x成为一个静态变量,这样它就是类本身的变量。 You don't need to instantiate objects in this case, but the variable will be common for all objects of type A (so you cannot have different values of x for different objects): 在这种情况下,您不需要实例化对象,但该变量对于A类型A所有对象都是通用A (因此不能为不同的对象使用不同的x值):
class A
    {
    public:
        static int x;
    };

    class B
    {
    public:
        int y;
        void FindY() { y = A::x + 12; }
    };

Assuming that A is correct and you cannot change it, ie x is a member variable, you will need an instance of an A in order to use its x member. 假设A是正确的并且您无法更改它,即x是成员变量,您将需要A的实例才能使用其x成员。

Thus said we can modify B but you need FindY() to take no parameters. 因此说我们可以修改B但你需要FindY()来不带参数。

Therefore we need to bring in the A with an earlier call and store it as a class member. 因此,我们需要使用较早的调用引入A并将其存储为类成员。

class B
{
public:
   A a;
   int y;
   void FindY() { y = a.x + 12; }
};

This is just an outline. 这只是一个大纲。 This is what is commonly done for "functor" classes where the function is operator() and takes a fixed number of expected parameters but we want more. 这是“functor”类通常所做的,其中函数是operator()并且需要固定数量的预期参数,但我们需要更多。 The whole of boost::bind is based on this principle. 整个boost :: bind基于这个原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM