• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
howto mix objc/c++ in a nice way?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

howto mix objc/c++ in a nice way?


  • Subject: howto mix objc/c++ in a nice way?
  • From: Simon Strandgaard <email@hidden>
  • Date: Thu, 10 Nov 2005 17:14:42 +0100

Hi objC++ coders,

How to let B.test() access A's internals, without
using (void*) and reinterpret_cast?

Thanks in advance.

--
Simon Strandgaard




// in .h file
struct AInner;
@interface A : NSObject {
	struct AInner* _inner;
}
-(id)init;
-(void*)inner;
@end

@interface B : NSObject {
}
+(void)test:(A*)a;
@end



// in .mm file
class AInner {
public:
	int _value;
};
@implementation A
-(id)init {
	_inner = new AInner;
	_inner->_value = 42;
	return self;
}
-(void*)inner {
	return _inner;
}
@end // A



// in same .mm file
@implementation B
+(void)test:(A*)a {
	void* ptr = [a inner];
	AInner* inner = reinterpret_cast<AInner*>(ptr);     // can this be improved?
	printf("the value is %i.", inner->_value);
}
@end // B



void maincode() {
	A* a = [[A alloc] init];
	[B test:a];
}
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: howto mix objc/c++ in a nice way?
      • From: Simon Strandgaard <email@hidden>
  • Prev by Date: Re: CGSessionCopyCurrentDictionary
  • Next by Date: Speeding up creation of NSDictionary from XML? (iTunes related)
  • Previous by thread: Re: Editable TableView, Bindings, and an Array -- almost
  • Next by thread: Re: howto mix objc/c++ in a nice way?
  • Index(es):
    • Date
    • Thread