ARC and static functions, free functions and C++ member functions

  • I would like to define a static function which returns a newly created object, for example:

    static Foo* makeFoo(const char* arg) {
      return [[Foo alloc] initWithArg:arg];
    }

    Do I need to use __attribute__((ns_returns_retained)), or is the above definition already correct as it is?

    Furthermore, are ARC rules fully applied within static functions and free functions? For example:

    Foo*  makeFoo(const char* str) __attribute__((ns_returns_retained));
    Foo*  makeFoo(const char* str) {
        NSString* s = [[NSString alloc] initWithCString:str encoding:Encoding];
        Foo* result = [[Foo alloc] initWithString:s];
        return result;
    }

    Is local variable 's' released properly?

    Furthermore is ARC fully applied within member functions of C++ classes?
    For example:

    // Objective-C++
    #import "Foo.h"

    class foo {
    public:
        foo(const std::string& s = "default") : _s(s) {}

        Foo* makeFoo() const {
        NSString* s = [[NSString alloc] initWithCString:_s.c_str() encoding:Encoding];
    Foo* result = [[Foo alloc] initWithString:s];
    return result;
        }

    private:
        std::string _s;
    };



    #include "foo.h"
    @implementation Bar {
        foo _foo;
    }
    - (Foo*) newFoo {
        return _foo.makeFoo();
    }
    @end

    If that works, how would I have to declare the C++ member function makeFoo(), considering two cases:
    1) makeFoo is inlined and defined within the class definition
    2) makeFoo is defined out of line within a foo.cpp module

    Thanks for help!

    Regards
    Andreas
previous month may 2012 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today