【问题标题】:Access variable from another class (in C++)从另一个类访问变量(在 C++ 中)
【发布时间】:2014-09-30 22:45:13
【问题描述】:

这可能是一个非常简单的问题,但是……就这样吧。 (提前致谢!)

我正在简化代码,以便于理解。我想使用在另一个类中计算的变量而不再次运行所有内容。

source.ccp

#include <iostream>
#include "begin.h"
#include "calculation.h"
using namespace std;
int main()
{
    beginclass BEGINOBJECT;

    BEGINOBJECT.collectdata();
    cout << "class " << BEGINOBJECT.test;

    calculationclass SHOWRESULT;
    SHOWRESULT.multiply();

    system("pause");
    exit(1);
}

begin.h

#include <iostream>
using namespace std;

#ifndef BEGIN_H
#define BEGIN_H

class beginclass
{
    public:
        void collectdata();
        int test;
};

#endif

begin.cpp

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

void beginclass::collectdata()
{
    test = 6;
}

calculation.h

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

#ifndef CALCULATION_H
#define CALCULATION_H

class calculationclass
{
    public:
        void multiply();
};

#endif

calculation.cpp

#include <iostream>
#include "begin.h"
#include "calculation.h"

void calculationclass::multiply()
{
    beginclass BEGINOBJECT;
    // BEGINOBJECT.collectdata(); // If I uncomment this it works...
    int abc = BEGINOBJECT.test * 2;
    cout << "\n" << abc << endl;
}

【问题讨论】:

  • 您可以使用静态成员或单例。
  • 不要使用静态成员或单例。了解如何从函数返回内容,以及如何将内容传递给函数。

标签: c++ class inheritance function-definition


【解决方案1】:

将成员函数multiply简单定义为

void calculationclass::multiply( const beginclass &BEGINOBJECT ) const
{
    int abc = BEGINOBJECT.test * 2;
    cout << "\n" << abc << endl;
}

并将其称为

int main()
{
    beginclass BEGINOBJECT;

    BEGINOBJECT.collectdata();
    cout << "class " << BEGINOBJECT.test;

    calculationclass SHOWRESULT;
    SHOWRESULT.multiply( BEGINOBJECT );

    system("pause");
    exit(1);
}

【讨论】:

  • 谢谢!它解决了我的代码中的问题,但我无法理解“(const beginclass &BEGINOBJECT)const”部分。你能解释一下吗?
  • @ramen 第一个 const 表示对 beginclass 类型对象的引用,该对象是常量,不能在函数中更改。第二个 const 表示函数不会更改调用它的对象,即对象 SHOWRESULT 本身不会更改。
  • 对不起,我有另一个疑问。如果在成员函数multiply 中计算出一个数组(比如说)yxz,那么我该如何在其他类中再次使用它?在正常情况下,我会输入public: int** yxz; void calculationclass::multiply();,它可能会被“提取”......你能给我另一个提示来解决这个int** yxz 需要吗?感谢您的耐心等待。
  • @ramen 对不起,我没明白你的意思。您可以在这里再问一个问题,并详细说明问题。
  • calculation.h 中,如果我尝试像这样添加一个整数(为了简化,忘记我之前说的数组..):class calculationclass { public: int yxz; multiply( const beginclass &amp;BEGINOBJECT ) const; } 然后在 calculation.cpp 如果我想使用,例如void calculationclass::multiply( const beginclass &amp;BEGINOBJECT ) const { yxz = 88*BEGINOBJECT.test; }。 yxz 未被识别,因此这个新值卡在类中......(希望我说清楚了。很难通过评论。)谢谢!
【解决方案2】:

在您的代码中beginclass 没有显式构造函数,因此将使用隐式定义的默认构造函数,该构造函数默认构造所有成员。因此,在构造之后 beginclass::test 要么是 0 要么是未初始化的。

您似乎想要避免多次致电beginclass::collectdata()。为此,您需要设置一个标志来记住是否已调用 beginclass::collectdata()。返回数据的成员函数首先检查此标志,如果未设置标志,则首先调用beginclass::collectdata()。另请参阅 CashCow 的答案。

【讨论】:

  • beginclass::test 将在 OP 的代码中未初始化。
【解决方案3】:

看起来您正在寻找某种惰性求值/缓存技术,即在第一次请求时计算一个值,然后存储以在随后返回它,而无需重新评估。

在多线程环境中,实现这一点的方法(使用新的标准线程库)是使用std::call_once

如果您在单线程环境中,并且只想从类中获取值,请为该值使用 getter。如果它不是以“惰性”方式计算的,即类立即计算它,您可以将该逻辑放在类的构造函数中。

对于“calc_once”示例:

class calculation_class
{
   std::once_flag flag;
   double value;

   void do_multiply(); 
   double multiply();
public:

   double multiply()
   {
       std::call_once( flag, do_multiply, this );
       return value;
   }
};

如果您希望 multiply 为 const,则需要使 do_multiply 也为 const 和 value 和 flag 可变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2023-04-07
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多