简体   繁体   English

C ++二进制范围解析运算符和类

[英]C++ Binary Scope Resolution Operator and Classes

Is there a way to use "block" class scope resolution in C++ so that I don't have to write the same boilerplate code for every function in my class' implementation file. 有没有一种方法可以在C ++中使用“块”类作用域解析,因此我不必为类的实现文件中的每个函数编写相同的样板代码。

I find it extremely repetitive to write the same class name and binary scope resolution operator (Classname::) when defining a function outside of the header file in C++. 我发现在C ++中的头文件之外定义函数时,编写相同的类名和二进制范围解析运算符(Classname::)非常重复。

In Objective-C I only need to include functions within the @implementation/@end block. 在Objective-C中,我只需要在@ implementation / @ end块中包含函数。

Objective-C Example: Objective-C示例:

// Buttons.h
@interface Buttons : UIView {
    NSMutableArray *buttonArray;
}
- (int)getNumberButtons;

// Buttons.m
#import "Buttons.h"
@implementation 
- (int)getNumberButtons 
{
    return [buttonArray count];
}
@end // End implemenation

C++ Example C ++示例

// Buttons.h
class Buttons {
public:
    int getNumberOfButtons() const;
protected:
    std::vector<Button> buttons;
};
// Buttons.cpp
#include "Buttons.h"
int Buttons::getNumberOfButtons() const {
    return buttons.size();
}

No, unless you would implement it all in the header in the class definition (which you usually shouldn't). 不可以,除非您可以在类定义的标头中全部实现(通常不应该这样做)。

Technically you could hack it with macros, but everyone else looking at the code would hate you for it. 从技术上讲,您可以使用宏对其进行破解,但是其他查看该代码的人都会讨厌您。 You'll have to get used to "the C++ way" here. 您必须在这里习惯于“ C ++方式”

Depending on what IDE you work with, there are usually tools (eg Visual Assist X for Visual Studio) that help you generate some of the boilerplate from a class definition. 根据您使用的IDE,通常会有一些工具(例如Visual Studio的Visual Assist X)可以帮助您从类定义中生成一些样板。

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

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