C ++ Matlab equivalent Annotation for class properties

Short version:
Consider the following pseudo code:

class Foo { private: abstract type myVar; } // This class is abstract 

How would you implement this behavior in standard C ++?


Long version:
I need to port a lot of Obj-oriented code from Matlab to C ++. Please note that I am the least experienced person in the world with Matlab, and I have not been using C ++ since 2007.

I searched a lot in this thread, but I could not find the right answer to my question. So here I am :)

Say you have this matlab class:

 classdef Foo < handle properties (Abstract, Dependent) A end properties B end methods (Abstract) computeA() end methods (Access = protected) function obj = Foo(bar) obj.B = Matlab.BlaBlaBla(bar) end end 

This class (I suppose) cannot be allocated "directly" because it is protected. Also, property "A" is abstract (for a moment, ignore the fact that also depends). MathWorks tells us that this means that:

  • Concrete subclasses must override abstract properties without the Abstract attribute and must use the same values ​​for SetAccess and GetAccess as those used in the abstract superclass.
  • Abstract properties cannot define methods for dialing or gaining access (see Methods for accessing properties) and cannot indicate initial values. a subclass that defines a specific property can create a set or obtain access methods and specify initial values.

So, how would you translate this behavior correctly in C ++? Would it be right if I did as it should? (In truth, this is not a bad design)

 class Foo { private: type A; type B; protected: virtual void computeA() = 0; Foo(type bar) { this.B = SomeFoo(bar); } } 

What I think (and maybe I'm wrong) is that if I do, you will need to

 class Subclass: Foo { protected: void computeA(){...} public: type A() { computeA(); return A; } //This is A getter that grants Dependent behavior } 

Or else get an error at compile time.

Am I mistaken? Is there a better way to do this? Also, is this the correct way to translate the Dependent keyword?

+4
source share
1 answer

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; //define a setA()=0 if you need it here protected: //I wouldn't force subclasses to have the "caching" of dependent properties, so no virtual void computeA() = 0; Foo(ClassBar const& bar) { this.B = ClassB(bar); /*Note that SomeFoo cannot be assigned to B*/ } } class Subclass: public Foo { private: ClassA A; public: ClassA getA() { ClassA A; /*compute A*/ return A; } } 

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() { /*compute A if required*/ 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); /*Note that SomeFoo cannot be assigned to B*/ } } 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 ...

+2
source

All Articles