Skip to Content
Objective-CObjective-C++

Objective-C++

风格匹配语言

在 Objective-C++ 源文件中,遵循你正在实现的函数或方法所用语言的风格。为了在混合使用 Cocoa/Objective-C 和 C++ 时减少不同命名风格之间的冲突,遵循正在实现的方法的风格。

对于 @implementation 块中的代码,使用 Objective-C 命名规则。对于 C++ 类方法中的代码,使用 C++ 命名规则。

对于 Objective-C++ 文件中类实现之外的代码,在文件内保持一致。

// GOOD: // 文件: cross_platform_header.h class CrossPlatformAPI { public: ... int DoSomethingPlatformSpecific(); // 在每个平台上实现 private: int an_instance_var_; }; // 文件: mac_implementation.mm #include "cross_platform_header.h" /** 一个典型的 Objective-C 类,使用 Objective-C 命名。 */ @interface MyDelegate : NSObject { @private int _instanceVar; CrossPlatformAPI* _backEndObject; } - (void)respondToSomething:(id)something; @end @implementation MyDelegate - (void)respondToSomething:(id)something { // 通过我们的 C++ 后端从 Cocoa 桥接 _instanceVar = _backEndObject->DoSomethingPlatformSpecific(); NSString* tempString = [NSString stringWithFormat:@"%d", _instanceVar]; NSLog(@"%@", tempString); } @end /** C++ 类的平台特定实现,使用 C++ 命名。 */ int CrossPlatformAPI::DoSomethingPlatformSpecific() { NSString* temp_string = [NSString stringWithFormat:@"%d", an_instance_var_]; NSLog(@"%@", temp_string); return [temp_string intValue]; }

项目可以选择使用 80 列行长度限制,以与 Google 的 C++ 风格指南保持一致。

Last updated on