First of all, I think it’s important to ask: what is the public interface of a class (what are its responsibilities, how does it interact with others)?
From your Matlab code, the answer is: the class defines properties A and B, as well as the computeA method. From my understanding of dependent properties, I doubt that computeA () should be publicly available (see Matlab Docs ). If the rest of your code needs this, you can leave it public, but I would try to reduce accessibility.
Now the concept of property does not exist in C ++. The funniest thing in Matlab is that the base class decides whether A.get, A.set, or both are available, as well as accessibility. I don’t know what is behind it, but it doesn’t seem too big for me. In C ++, I would translate the properties for getting / setting methods. See this question for a discussion of their implementation in C ++. Depending on your choice, you can implement independent properties as member objects or get / set methods.
Once you have identified the open interface of your method, I will try to start thinking about how to implement it. Note that the memory management of Matlab and C ++ is different (Matlab uses Copy on Write and takes care of memory management, none of this exists in pure C ++). In addition, value caching (as done with computeA and dependent properties) may be required in (slow object-oriented) Matlab code, but not necessarily in C ++. To avoid premature optimization, why not just do:
class Foo { public: ClassB B; virtual ClassA getA() = 0;
If you think that computing A is too slow, you can still "locally cache A" in a subclass:
class Subclass: public Foo { private: ClassA A; public: ClassA getA() { return A; } }
If you really want to save A in Foo, I would prefer to implement it as
class Foo { private: ClassA A; public: ClassB B; ClassA getA() { if (!A.initialized) A=computeA(); return A; }; protected: virtual ClassA computeA() = 0; Foo(ClassBar const& bar) { this.B = ClassB(bar); } } class Subclass: public Foo { protected: virtual ClassA computeA() {...} }
And don't forget to always think about whether you really want to pass a link (const) or value ...