Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



I am new to Objective-C++, and I am having some problems. I'm using 
a recent snapshot of GCC 4.2, for the record.

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<-----

What I'd like to do is to be able to pass an Objective-C object 
declared to be of type id, along with any statically typed 
Objective-C object, to the C++ method. Is this something that can 
be done in Objective-C++? How would one go about doing this?

Unfortunately, I don't have a Mac available to test this with. What 
behavior does Apple's version of GCC exhibit for the above 
Objective-C++ code? Does it compile?

Any help would be most appreciated. 

- Frederik Smith







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

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.