简体   繁体   English

如何从另一个.cpp源文件调用main.cpp中定义的方法?

[英]How to call a method defined in main.cpp from another .cpp source file?

First, this is a general question 首先,这是一个普遍的问题

Second: I thought of it because right now it complicated things in a project of mine. 第二:我想到了这一点,因为现在它在我的项目中使事情变得复杂。 Don't worry I got a workaround, but I'd still want to know if there had been another way of solving it. 不用担心我有解决方法,但是我仍然想知道是否还有另一种解决方法。

getting to the question: 提出问题:

I have my main.cpp. 我有我的main.cpp。 I defined some methods and functions in it. 我在其中定义了一些方法和功能。 I have some other files with other functions and stuff too, but now I need a function that works with temporary variables that are defined in the main.cpp. 我还有一些具有其他功能和其他东西的文件,但是现在我需要一个可与main.cpp中定义的临时变量一起使用的函数。 How can I extend the scope of such a variable so that I can use it in other files also? 如何扩展此类变量的范围,以便也可以在其他文件中使用它?

From classes, I know that fields can be private or public. 从课堂上,我知道字段可以是私有的也可以是公共的。 But what about this variable? 但是这个变量呢? Is it private? 它是私人的吗? public? 上市? something else? 还有什么吗 can I create a getter/setter for this variable? 我可以为此变量创建一个getter / setter方法吗? I can't even include the main.cpp in another since it would be recursive and I'd have main defined infinite times then ....) 我什至不能将main.cpp包含在另一个目录中,因为它将是递归的,然后我将main定义为无限次....)

The main questions would be: 主要问题是:

How can I access a variable defined in my main.cpp in another file? 如何在另一个文件中访问在main.cpp中定义的变量?

some example code: 一些示例代码:

in main.cpp: 在main.cpp中:

int var = 0;

int getVar() {
    return var;
}


void doVar() {
    // sth happens with var
}

int main() {
    MyObject myObj;

    doVar();
}

in MyObject.cpp: 在MyObject.cpp中:

class MyObject {
    void DoSth(){
        // NEEDS var from main.cpp, to do sth with it!
        // getVar() doesn't work!
        // doVar() doesn't work either!
    }
}

Forgive me if this is a previously asked or really stupid question, but I was really wondering about that just now 原谅我这是以前问过的还是真的很愚蠢的问题,但是我刚才真的很想知道

My workaround was that I made doVar and var to members of MyObject (ie now its all in the same file MyObject.cpp), but is this the only way? 我的解决方法是将doVar和var设置为MyObject的成员(即,现在都在同一文件MyObject.cpp中),但这是唯一的方法吗?

"Is it private / public / something else?" “是private / public /其他?”

Such is called a global variable , and it's publicly visible. 这就是所谓的全局变量 ,它是公开可见的。 Anyone can access it just providing a extern int var; 任何人都可以提供extern int var;来访问它extern int var; statement. 声明。

"can I create a getter/setter for this variable?" “我可以为此变量创建一个getter / setter吗?”

Yes, you can do (see later explanations) 是的,您可以这样做(请参阅后面的说明)

"I can't even include the main.cpp in another since it would be recursive and I'd have main defined infinite times then ....)" “我什至不能将main.cpp包含在另一个目录中,因为它将是递归的,然后我将main定义为无限次....)”

Of course you can't do this (besides you never want to include .cpp files anywhere). 当然,您不能这样做(除了您永远不想在任何地方包括.cpp文件)。
You need some header to declare this interface as being implemented externally, something like 您需要一些标头来声明此接口是在外部实现的,例如

VarInterface.hpp

#ifndef VARINTERFACE_HPP
#define VARINTERFACE_HPP

extern int var; // Optional, just encapsulating it with the functions is the
                // better choice
int getVar();
void doVar();

#endif // VARINTERFACE_HPP

And include it with your implementation in MyObject.cpp . 并将其包含在MyObject.cpp的实现中。


As for your comment : 至于你的评论

"what would it do? I just googled and came up with something similar to static , is that right? so what's the difference?" “这会做什么?我只是在Google上搜索并想出了与static类似的东西,对吗?那有什么区别?”

There's no private or public access scope policy for free functions (like with the C static keyword). 没有针对自由功能的privatepublic访问范围策略(例如使用C static关键字)。

Any function or global variable declaration like shown in the sample above is actually accessible, unless it's placed in an unnamed namespace . 像上面示例中所示的任何函数或全局变量声明实际上都是可访问的,除非将其放置在未命名命名空间中 The latter really restricts linker access to the translation units they are placed in: 后者实际上限制了链接器对它们所在的翻译单元的访问:

Another.cpp

namespace {
   // These functions and variables are exclusively accessible within
   // Another.cpp
   int var = 0;
   int getVar();
   void doVar();
}

But take the above just as a side note, since you want the opposite, as far I've understood from your question. 但是,就上述情况而言,我想从您的问题中了解到相反的情况,因为您想要相反的说法,因此请以上述说明作为补充。


"Can you create 'private' fields in it and somehow access them from another .cpp in this project?" “您可以在其中创建“私有”字段,并以某种方式从该项目中的另一个.cpp访问它们吗?”

If you want to hide the int var; 如果要隐藏int var; declaration/definition from other translation units (recommended), you can still do it like 其他翻译单位的声明/定义(推荐),您仍然可以像

VarInterface.hpp

#ifndef VARINTERFACE_HPP
#define VARINTERFACE_HPP

int getVar();
void doVar();

#endif // VARINTERFACE_HPP

MyObject.cpp

#include "VarInterface.hpp"

namespace {
   int var = 0;
}

int getVar() {
   return var;
}

void doVar() {
    // sth happens with var
}

main.cpp

#include "VarInterface.hpp"

int main() {
    MyObject myObj;

    doVar();
}

Declare any external variables or functions you need: 声明您需要的任何外部变量或函数:

extern int var;
int getVar();
void doVar();

either in the source file that uses them, or a header that can be included by any file that wants to use them. 可以在使用它们的源文件中找到,也可以在任何想要使用它们的文件中包含一个标头。

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

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