【问题标题】:How to make functions variables public, they are not in a class C++如何使函数变量公开,它们不在 C++ 类中
【发布时间】:2021-09-26 05:56:24
【问题描述】:

我想知道如何将函数的变量公开给其他函数。

例子:

void InHere
{
    int one = 1; // I want to be public
}
int main()
{
    InHere(); // This will set int one = 1
    one = 2; // If the variable is public, I should be able to do this
    return 0;
}

有人知道怎么做吗?我在搜索时发现的唯一内容是类,因为您可以看到类中没有任何内容,我不希望它们出现在一个类中。

非常感谢任何帮助!

【问题讨论】:

  • C++ 不能这样工作。如果您希望变量可以在单个函数之外访问,它们需要存在于类或命名空间中。除了InHere 之外,其他人无法知道one
  • 其他函数的公共变量是全局变量。

标签: c++ variables public


【解决方案1】:

在函数本地定义的变量通常无法在该函数之外访问,除非该函数显式提供指向该变量的引用/指针。

一个选项是函数显式返回一个引用或指向该变量的指针给调用者。如果变量不是static,则会给出未定义的行为,因为它在函数返回后不存在。

int &InHere()
{
     static int one = 1;
     return one;
}

void some_other_func()
{
     InHere() = 2;
}

如果变量one 不是static,这会导致未定义的行为,因为在这种情况下,变量仅在InHere() 被调用时才存在,并且在它返回时不再存在(因此调用者会收到一个悬空reference - 对不再存在的事物的引用)。

另一个选项是函数将指针或对变量的引用作为参数传递给另一个函数。

 void do_something(int &variable)
 {
      variable = 2;
 }     

 int InHere()
 {
      int one = 1;
      do_something(one);
      std::cout << one << '\n';    // will print 2
 }

缺点是这只提供对由InHere()调用的函数的访问。虽然在这种情况下变量不需要是static,但随着函数返回,变量仍然不复存在(所以如果你想以某种方式组合选项1和选项2,变量需要是static

第三种选择是在文件范围内定义变量,因此它具有静态存储持续时间(即它的生命周期与函数无关);

 int one;
 void InHere()
 {
      one = 1;
 }

 void another_function()
 {
      one = 2;
 }

 int main()
 {
     InHere();
        // one has value 1

     some_other_function();
        // one has value 2
 }

可以在任何可以看到变量声明的函数中访问全局变量。例如,我们可以这样做

 extern int one;             // declaration but not definition of one

 int one;                    // definition of one.  This can only appear ONCE into the entire program

 void InHere()
 {
      one = 1;
 }

并且,在其他源文件中

 extern int one;         // this provides visibility to one but relies on it
                         //   being defined in another source file 

 void another_function()
 {
      one = 2;
 }

 int main()
 {
     InHere();
        // one has value 1

     some_other_function();
        // one has value 2
 }

但要小心 - 全局/静态变量有很多缺点,在某种程度上,它们通常被认为是非常糟糕的编程技术。查看this link(以及从那里链接到的页面)了解一些问题的描述。

【讨论】:

    【解决方案2】:

    只需将变量设置为全局变量即可。然后您可以从其他功能访问它。

    int one;
    void InHere()
    {
        one = 1; // I want to be public
    }
    int main()
    {
        InHere(); // This will set int one = 1
        one = 2; // If the variable is public, I should be able to do this
        return 0;
    }
    

    如果你想在一个类中使用它,那么试试下面的代码

    #include <iostream>
    using namespace std;
    
    class My_class
    {
        // private members
    public: // public section
        // public members, methods or attributes
        int one;
        void InHere();
    };
    
    void My_class::InHere()
    {
        one = 1; // it is public now
    }
    int main()
    {
        My_class obj;
        obj.InHere(); // This will set one = 1
        cout<<obj.one;
        obj.one = 2; // If the variable is public, I should be able to do this
        cout<<obj.one;
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2017-04-12
      • 2019-07-27
      相关资源
      最近更新 更多