简体   繁体   English

[C ++]变量跨.cpp文件

[英][C++]Variables cross .cpp files

This is a silly question with something that must be an easy answer, but after hours of searching I cannot find the answer. 这是一个愚蠢的问题,有些问题一定是一个简单的答案,但是经过数小时的搜索,我找不到答案。 What I need to do is have a pair of .cpp files, say main.cpp and help.cpp that have a variable, vars1 that they share and can both change the value and detect when that value has been changed. 我需要做的是有一对.cpp文件,例如main.cpp和help.cpp,它们有一个变量vars1,它们共享并且可以更改值并检测何时更改了该值。 The way that would make sense to me is that I would simply declare the variable in a class inside a header file and include that header file in both .cpp files, but that doesn't seem to work. 对我来说有意义的方法是,我只需要在头文件中的类中声明变量,然后在两个.cpp文件中都包含该头文件,但这似乎不起作用。

Here is a copy of my code: 这是我的代码的副本:

#include <iostream>
#include <fstream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "variables1.h"

using namespace std;

int main(){
variables1 vars1;

do {
    cout << "Welcome\n If you need help, type 'yes' now\n";
    cin.getline(vars1.input, 1024);
    if (strcmp(vars1.input, "yes") == 0 || strcmp(vars1.input, "Yes") == 0){
        vars1.helpvar = true;
        cin.get();
    }
    else{
        cout << "Okay then, glad that you know your way around\n";
    }
    cin.clear();
    cout << "What would you like to do?\n";
    cin.getline(vars1.input, 1024);
    if (strcmp(vars1.input, "logon" ) == 0 ) {

    }
} while (0 == 0);
}

help.cpp: help.cpp:

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

using namespace std;

int help(){
    variables1 vars1;
    do {
        if (vars1.helpvar == true)
            cout << "detecting";
    } while (0 == 0);
}

variables1.h: variables1.h:

class variables1
{
public:
    bool helpvar;
    char input[1024];
};

Actually what you are doing is that for the main file and the help.cpp you are creating two different objects and are setting the helpvar variable for each of them separately. 实际上,您正在为主文件和help.cpp创建两个不同的对象,并分别为每个对象设置helpvar变量。 What you want is to have a single object that is used by both help.cpp and main to only modify a single instance of the helpvar variable. 您想要的是只有一个对象,help.cpp和main都使用该对象来仅修改helpvar变量的单个实例。

Change your help function to be along the lines of 更改您的帮助功能,使其与

int help(const variables1& myHelpobject ){
    if (myHelpobject.helpvar == true) {
            cout << "detecting";
    }
}

and then call the function in main as: 然后在main中将函数调用为:

help(vars1)

What you were doing before was creating a separate, independent, help object. 您之前所做的是创建一个单独的,独立的帮助对象。

Here we are creating the object in main and then passing a reference to it to the function. 在这里,我们在main中创建对象,然后将对它的引用传递给函数。

The technique to use depends on the purpose of your variable. 使用的技术取决于变量的目的。

If it is some sort of global parameters , that you have to use throughout all your code, the simplest is to define it as a global variable: 如果必须在所有代码中使用某种全局参数 ,则最简单的方法是将其定义为全局变量:

main file: 主文件:

variables1 vars1;  // outside all functions
int main(){
...
}

Either in variables1.h or in the other cpp files using the variable: 在variable1.h或其他使用该变量的cpp文件中:

extern variables1 vars1;  //outside all functions 

However the code to initialise and maintain these variables in a should also be defined in the class. 但是,也应该在类中定义用于初始化和维护这些变量的代码。 The constructor shall for example define the values by default, such as if help is enable or disabled. 例如,构造函数应默认定义值,例如启用或禁用帮助。

If your variables are for communicating between different parts of your code, and especially if the main goal of some code is to process the content of these variables, then should better make this clear by passing the variable as parameter (by reference (&) if the communication is bidirectional, or by value). 如果您的变量用于在代码的不同部分之间进行通信 ,特别是如果某些代码的主要目标是处理这些变量的内容,则最好通过将变量作为参数传递(如果引用,则使用通信是双向的或按值进行的)。

There are 2 main issues with the code as posted: 发布的代码有两个主要问题:

int help() is never run int help()永远不会运行

Something needs to call this function for it to run. 需要调用此函数才能使其运行。 There isn't anything doing that so regardless of the value of vars1.helpvar you are never going to see "detecting" output. 没有任何操作,因此无论vars1.helpvar的值如何,您都永远不会看到"detecting"输出。

Consider adding a help.hpp with the definition of the function and call the function from main . 考虑添加带有该函数定义的help.hpp并从main调用该函数。

vars1.helpvar is not shared between main and int help() vars1.helpvar在main和int help()之间不共享

Currently you have two instances of variables1 and helpvar is a member variable so each instance has a separate copy. 当前,您有两个variables1实例,而helpvar是成员变量,因此每个实例都有一个单独的副本。

You could either: 您可以:

  1. Make helpvar a static member of variables1 使helpvar成为variables1的静态成员
  2. Share once instance of variables1 between both main and help . mainhelp之间共享一次variables1实例。

The use of static variables is more likely give design problem later so I'd favour option 2. 使用静态变量更有可能在以后给设计带来问题,因此我赞成选项2。

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

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