简体   繁体   中英

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.

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++.

In Objective-C I only need to include functions within the @implementation/@end block.

Objective-C Example:

// 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

// 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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