【发布时间】:2011-09-09 11:12:22
【问题描述】:
最近我在代码中使用了不透明指针类型。我这样做是因为我想在我的 obj c 项目中使用 c++ 代码,而不必将每个文件都更改为 .mm。
我使用 c++ 代码的方式是我有一个不透明的指针指向我的 c++ 代码作为 .mm 文件的成员。所有的c++都隐藏在实现文件中。
在这个包含我的 c++ 的类中,我需要导入一个现有的类“MyClass”。我可以在实现类中很好地导入它,但是如果我尝试在标头中导入它,我会收到 c++ 错误,说“ISO C++ 禁止在没有类型的情况下声明 'CARingBufferCPPWrapper'”。
我“可以”只在 .mm 文件中编写方法并从标题中省略它,但我收到警告说我的 .mm 文件可能无法响应该方法。
其中很多内容都很新,所以我的术语可能有点不对劲。如果我能以任何方式澄清我的问题,请告诉我。
TLDR:X 类如何安全地调用 Y 类中的方法,而无需在 Y 类标头中声明该方法?
//我的标题
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#define kBufferLength 5120
//#define "Myclass.h"
typedef struct ringbufferobj * RingBufferOBJ;
RingBufferOBJ newRingBufferOBJ();
@interface CARingBufferCPPWrapper : NSObject {
RingBufferOBJ ringbuffer;
NSThread *producerthread;
int duration;
}
@property (nonatomic, retain) NSThread *producerthread;
@property(nonatomic)int duration;
//-(void)myclassfunction(MyClass *)classref
@end
//我的实现.mm
#import "CARingBufferCPPWrapper.h"
#import "CARingBuffer.h"
#import <AudioToolbox/AudioToolbox.h>
#import "MyClass.h"
struct ringbufferobj
{
CARingBuffer *ringbuffer;
AudioBufferList *inputbuffer;
Float64 firstInputSampleTime;
Float64 firstOutputSampleTime;
Float64 inToOutSampleTimeOffset;
BOOL producerthreadisrunning;
};
RingBufferOBJ newRingBufferOBJ(){
RingBufferOBJ ringbuffer=(RingBufferOBJ)malloc(sizeof(struct ringbufferobj));
return ringbuffer;
}
@implementation CARingBufferCPPWrapper
@synthesize producerthread;
@synthesize duration;
-(void)myclassfunction(MyClass *)classref
{
}
@end
【问题讨论】:
标签: objective-c oop