簡體   English   中英

C ++二進制范圍解析運算符和類

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

有沒有一種方法可以在C ++中使用“塊”類作用域解析,因此我不必為類的實現文件中的每個函數編寫相同的樣板代碼。

我發現在C ++中的頭文件之外定義函數時,編寫相同的類名和二進制范圍解析運算符(Classname::)非常重復。

在Objective-C中,我只需要在@ implementation / @ end塊中包含函數。

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 ++示例

// 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();
}

不可以,除非您可以在類定義的標頭中全部實現(通常不應該這樣做)。

從技術上講,您可以使用宏對其進行破解,但是其他查看該代碼的人都會討厭您。 您必須在這里習慣於“ C ++方式”

根據您使用的IDE,通常會有一些工具(例如Visual Studio的Visual Assist X)可以幫助您從類定義中生成一些樣板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM