gpt4 book ai didi

c++ - 在 iPhone xcode 应用程序中,是否可以声明具有全局范围的指针?

转载 作者:行者123 更新时间:2023-11-28 08:14:25 25 4
gpt4 key购买 nike

我想做的事:

#include "MyClass.h"MyClass *obj = new MyClass();

我在 main.mm、main() 内、我的 appDelegate.mm 和 application:didFinishLaunchingWithOptions 内尝试过。但这些似乎都不起作用。

否则我必须将指针传递给每个对象。这涉及另一个接口(interface)、更多方法调用和更多#includes。

谢谢

最佳答案

是的,这完全有可能。

请记住在要使用全局变量的翻译单元中使用 extern 关键字。

例如,在您的ma​​in.m 文件中:

NSString* yourGlobalStr = [[NSString alloc] initWithString:@"Global!"];

在你要使用它的地方,在other_file.m中放:

extern NSString* yourGlobalStr;

- (void) m
{
NSLog(yourGlobalStr);
}

关于c++ - 在 iPhone xcode 应用程序中,是否可以声明具有全局范围的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8110945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com