The @Ame code is correct, but there is no particular requirement to use shared_ptr here. I am very torn to use shared_ptr in a broad sense. This may be useful, but in my experience introduces a lot of subtle complexity. This is not a traditional C ++ approach. C ++ often prefers ownership of a solid object rather than shared ownership (which is a common model in ObjC). If you use shared_ptr , it is built in for Cocoa platforms, so you don't need boost. You can read C ++ Wrap - Take 2, Part 2 to understand some of the complexities around shared_ptr (this is a bit dated, and some of them are not applicable to the ARC code).
However, @Ame's approach is essentially correct. But you would usually use copying for simple properties, not shared_ptr . (This is especially true for lines that you also copy in most ObjC codecs.) For those who are looking for style guides, I usually recommend Google . This is not ideal, but it is very well considered, and it’s good to start with something that is known to work for many people before coming up with their own. (EDIT: see @Matthieu M. Comment below for a dissenting opinion.)
class MyClass { public: ... int num_entries() const { return num_entries_; } void set_num_entries(int num_entries) { num_entries_ = num_entries; } private: int num_entries_; };
Note that private: is true here. I do not agree with the use of @Ame protected: Just like ObjC, you should use accessors even inside classes, and definitely you should use them in subclasses. Providing subclasses with direct access to ivars is fragile. This requires subclasses to have special knowledge of their superclass.
For string properties and other simple or immutable objects, you should usually use the copy constructor, rather than something like shared_ptr . For more complex, mutable objects, C ++ usually encourages ownership of active objects rather than shared ownership. Thus, there should (in general) be one single entity responsible for the creation, management, and destruction of this other complex entity. All others should simply receive links from the owner of the object. They should never create or destroy the object itself.
It is not that separation or strict ownership is better than IMO. It's just that co-ownership is an ObjC way, and all the code works this way (and in that it is extremely elegant). Strict ownership is more of a C ++ way (since C ++ is said to have a “way”), and the attempt to share it is often fragile.