# Implicit cast from a primitive type

This topic is 2170 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello Andreas,

is it possible to register an implicit or a regular cast behaviours for primitive types? I'd like to be able to cast primitive types to strings without calling some specialized functions or adding overloads for that.

Correction: the name of this thread should say from a primitive type, not to.

##### Share on other sites
I've updated the topic title.

Implicit casts from primitives without function overloads are not yet supported. However, explicit casts are easily implemented by registering the appropriate constructor, e.g.:

 engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTIONPR(string_construct, (int, string*), void), asCALL_CDECL_OBJLAST); 

The script can then do the following:

 int a; string s; s = string(a); 

Implicit casts from primitives will eventually be implemented by allowing the compiler to implicitly find and call the appropriate constructors. I'll probably need to have a new behaviour for this though, i.e. asBEHAVE_IMPLICIT_CONSTRUCT, so the application can choose which constructors can be called implicitly or not.

##### Share on other sites
Well, yes, I'm aware of the explicit casts using constructors/factories, however what I had in my was something different:
 void SetProperty(cString name, cString value) { .. } ... int position = 100; self.setProperty('position', position); 
without having to do something like this:
 self.setProperty('position', cString(position)); 
or even this:
 self.setProperty('position', "" + position); 

##### Share on other sites
Until I have the implicit calls to the constructors implemented, what you want can only be implemented through function overloads, i.e:

 void SetProperty(cString name, int value) { ... } 

##### Share on other sites
yeah, figured that's a rather unpractical approach though, but thanks anyway