Reputation: 1457
I have two wrapper classes written in Objective-C++
for their equivalent C++ classes. Let's call them OABCClass
and OXYZCallbackInterface
. Now I have a method in C++ (ABCClass)
in which an argument is of a interface- XYZCallbackInterface
.
For example :
std::string methodWithArguments(std::string req, CommonNamespace::XYZCallbackInterface &callback);
In my Objective-C++ wrapper class for i.e. OABCClass
my method implementation for above mentioned C++ method looks like following:
-(NSString *)methodWithArguments(NSString*)req callback(OXYZCallbackInterface*)callback {
std::string res = cppClassVariable->methodWithArguments(req.UTF8String, callback);
}
Here I get error :
Non-const lvalue reference to type 'Common::XYZCallbackInterface' cannot bind to a temporary of type 'Common::XYZCallbackInterface *'
What is the correct way to use it? Any help is appreciated.
//Objective-C++ side
#ifndef OXYZCallbackInterface_h
#define OXYZCallbackInterface_h
#import <Foundation/Foundation.h>
//.h
@interface OXYZCallbackInterface : NSObject
-(bool)onResponseAvailable:(NSString* )response;
@end
#endif /* OXYZCallbackInterface_h */
//.mm
#import "OXYZCallbackInterface.h"
#include "Common/Common.Shared/OXYZCallbackInterface.h"
using namespace CommonNamespace;
@implementation OXYZCallbackInterface
- (instancetype)init
{
self = [super init];
return self;
}
-(bool)onResponseAvailable:(NSString* )response{
bool isResp = _objIPICCallback->onResponseAvailable(response.UTF8String);
return isResp;
}
@end
//C++ side
#pragma once
#include "DataTypes.h"
namespace CommonNamespace
{
class XYZCallbackInterface
{
public:
virtual ~ XYZCallbackInterface() {}
virtual bool onResponseAvailable(std::string response) = 0;
};
}
And OABCClass.mm implementation looks like:
-(NSString*) methodWithArguments(NSString*)req callback(OXYZCallbackInterface*)callback{
NSString* result = @"";
_pOABC -> methodWithArguments("", (__bridge XYZCallbackInterface*)callback);
// Error : Non-const lvalue reference to type CommonNamespace::XYZCallbackInterface cannot bind to a temporary of type CommonNamespace::XYZCallbackInterface *
return result;
}
Upvotes: 4
Views: 220
Reputation: 90531
You don't show your OXYZCallbackInterface
Objective-C++ class, so it's hard to be sure how exactly it "wraps" the XYZCallbackInterface
C++ interface. However, it's a sure thing that OXYZCallbackInterface
is not a XYZCallbackInterface
(in the object-oriented "is-a" sense). An instance of OXYZCallbackInterface
is not an instance of XYZCallbackInterface
(or any subclass). Therefore, you can't pass a pointer to an instance of OXYZCallbackInterface
to a function expecting a reference to an instance of XYZCallbackInterface
.
Your wrapper will have to provide a means to unwrap itself and return a pointer or reference to the original XYZCallbackInterface
object that it wraps. Let's say it has this signature:
- (XYZCallbackInterface*) original;
Then, you could call the C++ method like so:
std::string res = cppClassVariable->methodWithArguments(req.UTF8String, *[callback original]);
Upvotes: 1