简体   繁体   English

从另一个类C ++访问对象

[英]Access object from another class c++

I'm slowly learning c++ however whilst developing my program im having trouble trying to access and object Ive created. 我正在慢慢学习c ++,但是在开发程序时却难以访问Ive创建的对象。 My object is created on a page called source.cpp with 我的对象是在名为source.cpp的页面上创建的,

Tilemap background;

i have another header file, which in tern calls a function with needs to access 'background', the file is called 'player.cpp', however i cant figure out how to define it. 我有另一个头文件,该文件在tern中调用需要访问“背景”的函数,该文件称为“ player.cpp”,但是我不知道如何定义它。 Ive included the header file with the tilemap class but since the object is created elsewhere i don't know how to reference it. 我已经将标题文件包含在tilemap类中,但是由于该对象是在其他位置创建的,因此我不知道如何引用它。 Google isn't be my friend today and my searching has brought up nothing. Google今天不是我的朋友,我的搜索没有带来任何好处。

The line 线

Tilemap background;

written at file scope (ie not inside a function block) is a global variable definition . 在文件范围(即不在功能块内部)中编写的是全局变量定义 It allocates storage in the global data for a variable called background . 它在全局数据中为称为background的变量分配存储。

If you want to refer to this object from another C++ source file, you need to first declare the variable so the compiler knows its type. 如果要从另一个C ++源文件引用此对象,则需要先声明该变量,以便编译器知道其类型。

// In another .cpp file
extern Tilemap background;

void f()
{
    background.something();
}
Tilemap background;

placed in a global scope (let's say somewhere at the beginning of your source.cpp file) declares a global variable that is accessible only within the same compilation unit by default (in this case probably only within source.cpp ). 放置在全局范围内(例如,在source.cpp文件开头的某个位置)声明了一个全局变量,默认情况下,该变量只能在同一编译单元内访问(在这种情况下,可能仅在source.cpp内)。 In player.cpp , compiler doesn't know that variable background exists. player.cpp ,编译器不知道变量background存在。

One solution could be to put: 一种解决方案是:

extern Tilemap background;

in your player.cpp to let the compiler know that there is global variable of type Tilemap defined somewhere else. player.cpp中让编译器知道在其他地方定义了Tilemap类型的全局变量。

However, I find it better idea to avoid using global variables of this kind and try to "spread" (pass) variables / objects in form of arguments while calling some member functions ("methods"). 但是,我发现最好避免使用这种全局变量,而是在调用某些成员函数(“方法”)时尝试以参数的形式“传播”(传递)变量/对象。 After all, communication between objects is what OO programming is about... (I would provide some concrete example if I knew the context of this class / if you shared some code...) 毕竟,对象之间的通信就是面向对象编程的目的……(如果我知道此类的上下文/如果您共享一些代码,我将提供一些具体的示例……)

Ok so you are learning C++ then learn the good practices from the start and avoid C++ pitfalls. 好的,所以您正在学习C ++,然后从一开始就学习良好的做法,并避免C ++的陷阱。 Declaring and using C++ global objects that way can lead to very nasty behavior and long debugging sessions because you can't be sure the global object background which is declared at file global scope is initialized and properly constructed when you use it in another file. 以这种方式声明和使用C ++全局对象会导致非常讨厌的行为和冗长的调试会话,因为您不能确定在文件全局范围中声明的全局对象背景在另一个文件中使用时是否已初始化并正确构造。 When you use such an object ,let's call it B, in another global object A in another file you cannot be sure B is initialized before the A and that leads to very nasty bugs or if you are lucky a crash. 当您使用这样的对象时,我们将其称为B,在另一个文件中的另一个全局对象A中,您不能确定B在A之前被初始化,这会导致非常讨厌的错误,或者如果您很幸运会崩溃。 The solution then: make you object background a local static object ie local to a function like so: 然后的解决方案是:使对象背景成为局部静态对象,即像这样的局部函数:

TileMap& getBackground() {
  static TileMap background; //use of "camel case" for the class name
  return background; //return a reference to it 
}

That's it. 而已。 Anywhere you want to use background just call getBackground(). 任何您想使用背景的地方,只需调用getBackground()。 The first time you call it, background local static will be initialized and good to use. 第一次调用它时,背景局部静态将被初始化并且很好用。

Some will say that this technique is related to the singleton design pattern but this is not my intent here it is just the proper way to use global objects if you absolutely need to. 有人会说这种技术与单例设计模式有关,但这并不是我的目的,这只是在绝对需要时使用全局对象的正确方法。 C++ is a great language especially with the release of C++11 but it has some pitfalls you need need to be aware of. C ++是一种很棒的语言,尤其是在C ++ 11发行版中,但是它有一些陷阱,您需要注意。 Do yourself a favor: grab a copy of Effetive C++ it will teach almost everything you need to know to use the language properly 帮自己一个忙:获取Effetive C ++的副本,它将教您正确使用该语言所需的几乎所有知识

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

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