Skip to content

Instantly share code, notes, and snippets.

@uliwitness
Created August 6, 2015 13:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uliwitness/f9f96c7ab2c9a84db5e9 to your computer and use it in GitHub Desktop.
Save uliwitness/f9f96c7ab2c9a84db5e9 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#include <iostream>
@interface ULIFoo : NSObject
@end
@implementation ULIFoo
-(id) init
{
self = [super init];
if( self )
std::cout << "Created ULIFoo" << std::endl;
return self;
}
-(void) dealloc
{
std::cout << "Destroyed ULIFoo" << std::endl;
}
@end
class FooBase
{
public:
FooBase() { std::cout << "Created FooBase" << std::endl; };
virtual ~FooBase() { std::cout << "Destroyed FooBase" << std::endl; };
};
class Foo : public FooBase
{
public:
Foo() { std::cout << "Created Foo" << std::endl; };
virtual ~Foo() { std::cout << "Destroyed Foo" << std::endl; };
void doit() { std::cout << "Do It!" << std::endl; }
ULIFoo* mFoo;
};
class WeakFoo : public FooBase
{
public:
WeakFoo() { std::cout << "Created WeakFoo" << std::endl; };
virtual ~WeakFoo() { std::cout << "Destroyed WeakFoo" << std::endl; };
void doit() { ULIFoo* aFoo = mWeakFoo; std::cout << "Do It: " << (unsigned long long)mWeakFoo << std::endl; }
__weak ULIFoo* mWeakFoo;
};
void TestStrong()
{
Foo cppFoo;
@autoreleasepool
{
ULIFoo * objcFoo = [ULIFoo new];
cppFoo.mFoo = objcFoo;
}
cppFoo.doit();
}
void TestWeak()
{
WeakFoo weakFoo;
{
@autoreleasepool
{
ULIFoo * objcFoo = [ULIFoo new];
weakFoo.mWeakFoo = objcFoo;
weakFoo.doit();
}
}
weakFoo.doit();
}
int main(int argc, char *argv[])
{
TestStrong();
std::cout << "Strong done!" << std::endl << std::endl;
TestWeak();
std::cout << "Weak done!" << std::endl;
}
@uliwitness
Copy link
Author

Build this with

clang CppARCStrongWeakTest.mm -o CppARCStrongWeakTest -ObjC++ -std=c++11 -fobjc-arc -lstdc++ -framework Foundation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment