f



[Objective-C++] @defs, objective-c classes, and pod types

Can we assume that objective-C classes are C++ pod types when we do
something like the following?

@interface Foo {
   //...
}

//...
- (bool) isEqualToFoo:(Foo*) foo;
@end

class FooWrapper {
   @defs(Foo)
   
   public:
      // no constructors as we must preserve podness
      operator Foo*() { return this; }
      bool operator==(FooWrapper& rhs) { return [this
isEqualToFoo:rhs]; }
};

-- 
I am only a mirage.
0
kelvSYC
6/5/2005 5:42:41 AM
comp.sys.mac.programmer.help 4653 articles. 2 followers. Post Follow

1 Replies
778 Views

Similar Articles

[PageSpeed] 52

kelvSYC <kelvSYC@no.email.shaw.ca> wrote:
> Can we assume that objective-C classes are C++ pod types when we do
> something like the following?

ObjC classes are just C structs with funny semantics, so they should obey 
PODness. However....

> @interface Foo {
>   //...
> }
> 
> //...
> - (bool) isEqualToFoo:(Foo*) foo;
> @end
> 
> class FooWrapper {
>   @defs(Foo)
>   
>   public:
>      // no constructors as we must preserve podness
>      operator Foo*() { return this; }
>      bool operator==(FooWrapper& rhs) { return [this
> isEqualToFoo:rhs]; }
> };

This is almost certainly a bad idea for other reasons. Objective-C classes 
really, really, really, really, really like to live on the heap. Here, 
you're making it live wherever your FooWrapper lives, which is probably 
going to be on the stack. You'd probably be much better off making 
FooWrapper wrap a pointer to Foo. That way you don't have to care about 
PODness, you can write a single wrapper to handle arbitrary ObjC classes, 
etc.
0
Michael
6/5/2005 11:10:06 AM
Reply: