Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

C++ to CLR constructor or operator?

Joined
Aug 14, 2009
Messages
2,304
Reaction score
1,189
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] ( )

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 ( ):
Code:
_cmd->_open_guild_stage = gcnew OpenAllGuildStage(&cmd->_open_guild_stage());
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
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.
 
Joined
Aug 14, 2009
Messages
2,304
Reaction score
1,189
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:
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:
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
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 . 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?
GigaToni - C++ to CLR constructor or operator? - RaGEZONE Forums


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 [STRIKE]waste memory[/STRIKE] create copies of the object on all conversions / changes.
 
Last edited:
Back
Top