[英]Mixing Objective C++ and C++ in a bi-directional way
Suppose I have two objective c++ objects that each wrap a native c++ object given: 假设我有两个客观的c ++对象,每个对象都包装一个给定的本地c ++对象:
A, B = objective c++ object types A,B =目标C ++对象类型
Acpp, Bcpp = c++ object types ACPP,BCPP = C ++对象类型
In B.mm 在B.mm
#import "Bcpp.h"
#import "B.h"
@interface B ()
{
Bcpp myBcpp; // declare instance c++ variable of type Bcpp
}
@end
In A.mm 在A.mm
#import "Acpp.h"
#import "A.h"
@interface A ()
{
Acpp myAcpp; // declare instance c++ variable of type Acpp
}
@end
@implementation A
// method to return an instance of B from an instance of A (self)
- (B)GetBfromA
{
Bcpp *bfroma = myAcpp.GetBfromA(); // return c++ object
// How do i find the objective C++ object B from its wrapped c++ instance bfroma?
}
@end
The reason for doing this is we have a mature c++ data structure and we wish to wrap it with objective c++ objects. 这样做的原因是我们拥有成熟的c ++数据结构,并且希望将其与目标c ++对象一起包装。 Is the the best way?
是最好的方法吗? And if it is, how do we solve the reverse mapping problem?
如果是这样,我们如何解决反向映射问题?
EDIT: Thank you to the early responders but I have a more tricky situation that I implied above. 编辑:谢谢早期的响应者,但是我在上面暗示了一个更加棘手的情况。 Suppose the function GetBFromA() returns an instance of Bcpp that had already been declared (as an instance variable of an instance of B).
假设函数GetBFromA()返回一个已经声明的Bcpp实例(作为B实例的实例变量)。 So I am holding a pointer to a Bcpp object that is itself an instance variable of an objective C++ object of type B. How do I find the instance of B from the instance of Bcpp?
因此,我持有一个指向Bcpp对象的指针,该对象本身就是类型为B的目标C ++对象的实例变量。如何从Bcpp实例中找到B的实例?
What you probably need to do is to be able to create a B
from a Bcpp
. 您可能需要做的是能够从
Bcpp
创建一个B
So B
will need to be amended to have an -initWithBcpp:
method: 因此,将需要对
B
进行修改,使其具有-initWithBcpp:
方法:
- (id)initWithBcpp:(Bcpp*)bcpp
{
self = [super init];
if (self != nil)
{
myBcpp = *bcpp;
}
return self;
}
Then, in GetBFromA
, you'll need to create a B
from the Bcpp*
: 然后,在
GetBFromA
,您需要根据Bcpp*
创建一个B
:
- (B*)GetBfromA
{
Bcpp *bfroma = myAcpp.GetBfromA(); // return c++ object
B* result = [[B alloc] initWithBcpp:bfroma];
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.