C++ 如何访问与其他类不同的类中的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4507822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:36:14  来源:igfitidea点击:

How to access variables in different class from other class?

c++visual-c++

提问by Ramilol

Let just say that we have two classes, Aand B.
Here is code for both of them

假设我们有两个类,AB
这是他们两个的代码

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.
some how i need to find x in function FindY().
I'm going with the static method but in my case i get this error.

问题是我想访问 x 但我不想将任何内容作为参数传递给我的函数 我查看了朋友和继承,但两者似乎都没有帮助我,如果我错了,请纠正我。
我需要如何在函数中找到 x FindY()
我将使用静态方法,但就我而言,我收到此错误。

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.objHere is how i declared it

错误 2 错误 LNK2001:未解析的外部符号“public: static classstd::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);

回答by villintehaspam

Three different approaches:

三种不同的方法:

  1. Make B::FindY take an A object as a parameter

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

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

    class B : public A {
    public:
      void FindY() { y = x + 12; }
    };
    
  1. 使 B::FindY 以 A 对象作为参数

    class B {
    public:
      void FindY(const A &a) { y = a.x + 12; }
    };
    
  2. 将 A::x 设为静态

    class A {
    public:
      static int x;
    };
    class B {
    public:
      void FindY() { y = A::x + 12; }
    };
    
  3. 让 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 在他的回答中还指出了更多的方法来做到这一点。

回答by Frerich Raabe

As it is, xis not a variable of the class Abut a variable of objects ("instances") of class A. There are at least two ways to make xaccessible from B::findYwithout passing anything to the function:

事实上,它x不是类A的变量,而是类的对象(“实例”)的变量A。至少有两种方法x可以在B::findY不向函数传递任何内容的情况下访问:

  • Instantiate an object of class Ainside the B::findYfunction:
  • AB::findY函数内部实例化一个类的对象:
    class B
    {
    public:
        int y;
        void FindY() { A a; y = a.x + 12; }
    };
  • Make xa static variable, so that it's a variable on the class itself. 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 xfor different objects):
  • 创建x一个静态变量,使其成为类本身的变量。在这种情况下,您不需要实例化对象,但该变量对于所有类型的对象都是通用的A(因此您不能x为不同的对象设置不同的值):
    class A
    {
    public:
        static int x;
    };

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

回答by CashCow

Assuming that A is correct and you cannot change it, i.e. 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. The whole of boost::bind is based on this principle.

这只是一个大纲。这是通常为“函子”类所做的,其中函数是 operator() 并采用固定数量的预期参数,但我们想要更多。整个 boost::bind 就是基于这个原理。