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 
  test.mm:33: note: candidates are: void printer::print_Test(Test*)
  test.mm:53: error: no matching function for call to 
  test.mm:37: note: candidates are: void 

Here is the test.mm code in question:


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

@interface Test : Object {
  int i;

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


@implementation Test

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

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


class printer {
    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;

  return 0;


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 


[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

Get the best prices on SSL certificates from Hushmail

More information about the Gcc-help mailing list