Objective-C++: passing Objective-C objects to C++ methods?

frederiksmith1942@mac.hush.com frederiksmith1942@mac.hush.com
Sun Nov 12 05:15:00 GMT 2006


I've run into some problems with Objective-C++ while using the gcc-
4.2-20061107 snapshot.

Passing a statically typed Objective-C object to a C++ function 
with a parameter of type id works fine. However, I get compilation 
errors when passing the same statically typed Objective-C object to 
a method of a C++ class that has a parameter of type id.

I'm also getting errors when (in this case) I'm passing Objective-C 
classes of type Test, but declared to be of type id, to C++ object 
methods expecting an argument of type Test*.

I'm getting these errors:
  test.mm: In function 'int main(int, char**)':
  test.mm:51: error: no matching function for call to 
'printer::print_Test(objc_object*&)'
  test.mm:33: note: candidates are: void printer::print_Test(Test*)
  test.mm:53: error: no matching function for call to 
'printer::print_id(Test*&)'
  test.mm:37: note: candidates are: void 
printer::print_id(objc_object*)

Here is the test.mm code in question:

----8<-----

#import <stdio.h>
#import <objc/Object.h>

@interface Test : Object {
  int i;
}

-(id) init:(int)n;
-(id) print;

@end


@implementation Test

-(id) init:(int)n {
  i = n;
  
  return self;
}

-(id) print {
  printf("%d\n", i);
  
  return self;
}

@end


class printer {
  public:  
    void print_Test(Test* test) {
      [test print];
    }

    void print_id(id test) { 
      [test print];
    }
};

int main(int argc, char** argv) {

  Test* test_Test = [[Test alloc] init:100];  
  id test_id = [[Test alloc] init:200];

  printer p;

  p.print_Test(test_Test);
  p.print_Test(test_id);
  
  p.print_id(test_Test);
  p.print_id(test_id);
  
  return 0;
}

----8<-----

While I can't test it myself at the moment, this code reportedly 
works fine with Apple's compiler [1][2]. Is this a bug/restriction 
with GCC's implementation of Objective-C++, or is the above code 
incorrect?

   Frederik


References
[1] http://lists.apple.com/archives/Cocoa-dev/2006/Nov/msg00544.html
[2] http://lists.apple.com/archives/Cocoa-dev/2006/Nov/msg00545.html






Concerned about your privacy? Instantly send FREE secure email, no account required
http://www.hushmail.com/send?l=480

Get the best prices on SSL certificates from Hushmail
https://www.hushssl.com?l=485



More information about the Gcc-help mailing list