C++ to CLR constructor or operator?

Results 1 to 4 of 4
  1. #1
    Moderator GigaToni is offline
    ModeratorRank
    Aug 2009 Join Date
    GER / FRLocation
    2,041Posts

    C++ to CLR constructor or operator?

    I'm currently trying to decide whats better.. [In terms of easy of understanding, not in terms of performance. Since both would be equally as fast] (https://github.com/exmex/HC/issues/3)

    Using the constructor to do the conversion:
    Code:
    HeroArousal(const up::hero_arousal* arousal)
    Or use an overloaded operator for that.
    Code:
    static operator HeroArousal^(const up::hero_arousal* arousal)
    Constructor of CLR Class

    Method 1:
    Code:
    SetMoney(const up::set_money* money)
    {
        _type = (price_type)Convert::ToInt32(money->_type());
        _amount = Convert::ToInt32(money->_amount());
    }
    Method 2:
    I'd have to create the class, and return it like this:
    Code:
    static operator Up_GmCmd^(const up::gm_cmd* cmd)
    {
        Up_GmCmd^ _cmd = gcnew Up_GmCmd();
        return _cmd;
    }
    Conversion of class

    Method 1:
    Code:
    _cmd->_open_guild_stage = &cmd->_open_guild_stage();
    Method 2 (With Syntax Highlighting):
    Code:
    _cmd->_open_guild_stage = gcnew OpenAllGuildStage(&cmd->_open_guild_stage());


  2. #2
    C++ to CLR constructor or operator? Future is offline
    SupervisorRank
    Dec 2011 Join Date
    2,130Posts

    Re: C++ to CLR constructor or operator?

    What I'd do is the constructor way. After all the object is being constructed with this parameter. But you can do the operator on top of that and leave yourself the choice for later on. Would look like this on the example of the up msg:

    Code:
    static operator Up_GmCmd^(const up::gm_cmd* cmd)
    {
        return gcnew Up_GmCmd(cmd);
    }
    With the conversion code inside the Up_GmCmd constructor ofc.

    Another option would be having an update method of some sort with the cmd parameter. This way you can set it in the constructor, operator and when dealing with an object reference. Would save you runtime when you can get rid of the extra allocation. Unless you want to follow the C# like convention where new objects are returned by member methods. It does seem weird to me to do this in an operator though. These are usually used to update the object itself rather than return a new one.


    Quote Originally Posted by A Wise Man
    P-Servers are NOT dead. Bugs need squishing. Quests need fixing. Unfortunately, majority of people don't know the difference between a computer and a toaster so...

  3. #3
    Moderator GigaToni is offline
    ModeratorRank
    Aug 2009 Join Date
    GER / FRLocation
    2,041Posts

    Re: C++ to CLR constructor or operator?

    Thanks @Future I'll do it the constructor way. Since it seemed to me that it is the "verbose" way. Having this:
    Code:
    _cmd->_open_guild_stage = &cmd->_open_guild_stage();
    is looking weird. It looks like I'm assigning the pointer of _open_guild_stage to _open_guild_stage. Which is definitely not what it's doing! It's taking the pointer and converting it, and that is not clear.

    But to be honest, having both constructor + operator just seems wasteful on my time. After all I have to create ~70+ Classes for that thing. Even though it's not a huge amount of code, its alot when redoing it ~70 times. Since the class leaves CLR land as soon as possible!
    What I mean by that is, that the class Up_GmCmd will only have "readonly" fields. So set on constructor and never modified. Since the only purpose of the CLR library is converting the C++ data structure from Protobuf to C# land because protobuf didn't support C# until v3, and the whole code depends on v2.

    But don't quite understand this sentence:
    Quote Originally Posted by Future View Post
    It does seem weird to me to do this in an operator though. These are usually used to update the object itself rather than return a new one.
    Since all conversion operators return new objects don't they? At least thats how I did it always. Remember I'm just talking about conversion here. Not other operators like == or /.
    Last edited by GigaToni; 2 Weeks Ago at 04:09 PM.

  4. #4
    C++ to CLR constructor or operator? Future is offline
    SupervisorRank
    Dec 2011 Join Date
    2,130Posts

    Re: C++ to CLR constructor or operator?

    Quote Originally Posted by GigaToni View Post
    But don't quite understand this sentence:

    Since all conversion operators return new objects don't they? At least thats how I did it always. Remember I'm just talking about conversion here. Not other operators like == or /.
    I was referring to assignment-like operators. These usually perform the assignment and return a self reference by C++ convention. I rarely see a "conversion operator" but they are practically assigning, aren't they?

    Code:
    Object& Object::operator= (const Object& other)
    {
        // assignment operations
        this->field1 = other.field1;
        this->field2 = other.field2;
    
        // return of self reference
        return *this;
    }
    I would recommend doing assignment operators like that in C++. If you still want to have a copy of the object, it's a lot more readable and clear to do it in two steps.

    Code:
    Object newObj = other;
    This is where C++ style indeed differs from C# style where it's common to waste memory create copies of the object on all conversions / changes.
    Last edited by Future; 2 Weeks Ago at 08:28 PM.


    Quote Originally Posted by A Wise Man
    P-Servers are NOT dead. Bugs need squishing. Quests need fixing. Unfortunately, majority of people don't know the difference between a computer and a toaster so...



Advertisement