MAXON Data Type and explicit constructors



  • Is it possible to define an explicit constructor for Custom Data Types?
    In the examples provided here, the triplet is configured after allocating it.

    I would like to initialize my type upon creation. Is this possible?


  • Global Moderator

    Hi mp5gosu, thanks for reaching us.

    I'm a bit confused, since reading the topic and the documentation reference, it's unclear the question about explicit constructor on CustomDataType. MAXON Data Type and CustomDataType - and the related CustomDataTypeClass - address different needs in terms of development.
    Whilst the former is a convenient way to define and register new data structures inside the MAXON AP, used in conjunction with MAXON::DataDictionary, the second is the base class from which Cinema CustomDataType(s) are inherited from, used in conjuction with BaseContainers, and which are used to stored the data from the respective CustomGui(s) (think about the Gradient custom data type and the GradientGui)

    That said, can you please elaborate what do you mean by Is it possible to define an explicit constructor for Custom Data Types?

    Best, Riccardo



  • Hey Riccardo,

    Sorry for not being precise.
    I'm talking about the new Maxon API and its data containers, not the classic API at all.
    As far as I understand the maxon::DataDictionary and maxon::Data types, they are the replacements for both legacy types, BaseContainer and GeData.
    Now I wanted to have a class being a registered as a new Data type as written here, so I can use it with a DataDictionary

    However, I want the type to be initialized with a certain value. Let me illustrate it on the SDK example:

    // This example defines a custom class and declares it as a MAXON data type.
    #include "maxon/datatype.h"
    #include "maxon/string.h"
    // ------------------------------
    // Triplet of maxon::Int values.
    // ------------------------------
    class IntegerTriplet
    {
    maxon::Int _val; // Keep it private
    
    public:
      explicit IntegerTriplet(const maxon::Int val) : _val(val) {} // This gives a compiler error, complaining about that there's no default constructor available.
    };
    // register as MAXON data type
    MAXON_DATATYPE(IntegerTriplet, "net.maxonexample.datatype.integertriplet"); 
    

    And usage:

    IntegerTriplet triplet(30);
    // ...
    

    Can be that I misunderstood the concept of the new MAXON Data types, and I'd be really thankful if you can shed some light onto this.


  • Global Moderator

    @mp5gosu said in MAXON Data Type and explicit constructors:

    // This gives a compiler error, complaining about that there's no default constructor available.

    As suggested by the compiler simply make the default constructor living beside your constructor and the compiler won't complain anymore :)

    class IntegerTriplet
    {
    private:
      maxon::Int _val;
    
    public:
      IntegerTriplet(){}
      explicit IntegerTriplet(const maxon::Int val) : _val(val) {} 
    };
    

    Cheers, Riccardo



  • Of course, but doesn't this make the explicit constructor useless? With that approach the user is still able to instantiate the class without initializing the variable on creation correctly.

    At least when testing this, I was able to instatntiate the class without the need for an argument, so I assume that the explicit constructor simply gets omitted.


  • Global Moderator

    Hi mp5gosu, thanks for following up.

    I see your point, but actually you can still initialize the member variables to a "reasonable" default value helping to identify cases when the class was instantiated without any explicit value.

    A final, additional, side note about initialization: considering that constructors can't return any "evidence" of failure when allocating members we advise against initializing members directly in the constructor.
    We rather suggest implementing an additional Init() method where the members' initialization takes place as shown in the code snippet in the Entity Creation section.

    Best, Riccardo



  • Thanks Riccardo!

    Now I understand - thanks to your side note. :)
    And of course I understand, that this goes hand in hand with your guidelines.

    Cheers,
    Robert