C++ and Objective-C both require forward declaration of the methods and fields on a class. Objective-C lets you hide the existence of internal/private methods & fields altogether:
// MyObject.h
#ifndef _MyObject_h_
#define _MyObject_h_
#import <Foundation/Foundation.h>
@interface MyObject : NSObject {
NSInteger publicIvar;
}
- (void)publicMethod;
@end
#endif
// MyObject+Internal.h
#ifndef _MyObject_Internal_h_
#define _MyObject_Internal_h_
#import "MyObject.h"
@interface MyObject () {
NSInteger internalIvar;
}
- (void)internalMethod;
@end
#endif
// MyObject.m
#import "MyObject+Internal.h"
@implementation MyObject {
NSInteger privateIvar;
}
/* method implementations */
@end
If the project is configured such that dependents can’t import MyObject+Internal.h
, they can’t use the symbols from that extension. However, C++ doesn’t allow this; all fields and methods must be declared in one place. Dependents are able to see things that they can’t use.
In a language like these that requires forward declarations, what are the advantages of requiring private functionality to be declared in a public header?