0

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();
}
Paul Solt
  • 8,375
  • 5
  • 41
  • 46
  • Isn't this what Objective-C++ is for? Letting you use C++ code in Objective C programs? – Mike DeSimone Dec 03 '10 at 16:04
  • Is not the C++ way shorter anyway. At 21 characters `@implementation@end` You need four methods on a class with a name of length 4 characters before C++ becomes more onerous to type. – Martin York Dec 03 '10 at 16:12
  • @Mike I don't think you understand the question, it's not about using C++ code in Objective-C. I like Objective-C because I have less boilerplate code to write. I am using both Objective-C/C++ in my current project, and I'm just frustrated with the number of errors I run into related to the same boilerplate code. – Paul Solt Dec 04 '10 at 01:40
  • @Martin My example was simple, when you have a large class name and many methods it becomes much longer and repetitive. For instance one of my classes is named GenePoolEvolved. The issue comes from the fact that I can't just copy/paste a function definition from the header to the implementation file in C++ without modifications. However, I can do that in Objective-C, so it's more efficient to write the code correctly the first time. – Paul Solt Dec 04 '10 at 01:43

1 Answers1

3

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 (e.g. Visual Assist X for Visual Studio) that help you generate some of the boilerplate from a class definition.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • I find it a bit frustrating, but I can deal with it. I'm using the test driven approach, so I don't stub out the entire class. I work on only one or two method stubs at a time, write the unit tests, and then the implementation. – Paul Solt Dec 04 '10 at 01:37
  • @Paul: With decent tools this is no big problem: you could e.g. declare a method and choose the "generate implementation" entry in the context menu. – Georg Fritzsche Dec 04 '10 at 01:49
  • I'm not aware of a generation mechanism for Xcode. I'm primarily working on the Mac side of things, I don't have nice things like Visual Studio. – Paul Solt Dec 04 '10 at 02:08
  • @Paul: Right, neither am i. Maybe the Xcode plugin interface could be used to do similar things? – Georg Fritzsche Dec 04 '10 at 16:52