# Pointers, references, values between c++ and AngelScript

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

## Recommended Posts

Hi,

I already managed to pass a string and a string array to AS from my c++ code, but I'm not sure if I did it right (I think I passed as value from c++ to as).
 //AS code void bar(string str, string[] strArr){} //in C++ code std::string str = "foo"; CScriptArray* strArr; //initilaise array here SetArgObject(0, str); SetArgObject(1, strArr) 

Can someone give me a short overview of all the pointer, reference, @, *, &, in, out, inout operators and a short example when to use which in both languages when passing variables between the two (C++ and AS)? And how to pass variables if I only can acces them through a const reference (const string&)?
I am especially interested in string and array types.

I find this whole passing of variables confusing.

##### Share on other sites
It can indeed be a bit confusing. Here's a few tips on how to think of the differences:

In C++ a pointer and a reference is essentially the same thing, in fact in the memory they are represented exactly the same way, i.e. as an address to something. The only difference (besides syntax) is that a pointer can be null, but a reference cannot (well, it can, but is not supposed to be).

A reference in AngelScript is an exact match to the C++ reference. The 'in', 'out', and 'inout' keywords added in the AngelScript syntax tells the AngelScript compiler how the value pointed to by the reference will be used. An 'in' reference is used as an input parameter to the function, i.e. the function will read from the reference but will not update it. An 'out' reference is contrary, the parameter will be updated by the function to return a value to the calling function. To C++ these keywords don't mean anything, but they are very important to AngelScript in order to keep the script from doing things it mustn't do (sandboxing).

An object handle in AngelScript is represented in memory as an ordinary C++ pointer, but the object it points to is a reference counted object. When a C++ function receives a handle from a script in a parameter it is responsible for releasing that reference when it is done with it. Likewise, when a C++ function returns a handle to the script it must make sure to increment the reference count.

If a C++ function takes a parameter as 'const string &' then you will want to register it as 'const string &in'. If the C++ function takes a 'string &' and is really meant to be an output parameter then you want to register it as 'string &out'. If the C++ function takes a 'string &' and uses the parameter both as input argument and output argument, then you will need to implement a wrapper function as it is not allowed to register a value type as 'string &inout' (because it isn't possible to always guarantee the lifetime of the reference in this case).

For reference types, like the array, you will usually use either @ or &. Use @ if it is a valid scenario to have null instead of an object, and & if it is not. If you do not use either of the two, it means that you're passing the object by value, which makes the AngelScript compiler make a copy of the object (which can be hurtful to the performance if the object is very complex).

Here's a pair of articles of interest in the manual:

Understanding datatypes in AngelScript and C++
Understanding object handles

I hope that helps to clarify things a bít.

##### Share on other sites
Thank you very much, this already helps a lot.

Just so that i got it right:
In Angelscript code I can NEVER have a reference "type&" like in c++? I can only have "type &in" (or &out, &inout)?
And AngelScript code does NOT support the asterisk pointer (*), only the "@", which works basically the same?

And a new question:
When should I use SetArgObject and when SetArgAddress? Do they HAVE TO match the function declaration in the AS code (like Object for value types and Address for &in or @type)?

##### Share on other sites
You can have parameter references without in, out or inout by setting the asEP_ALLOW_UNSAFE_REFERENCES engine property.

##### Share on other sites

Thank you very much, this already helps a lot.

Just so that i got it right:
In Angelscript code I can NEVER have a reference "type&" like in c++? I can only have "type &in"  (or &out, &inout)?
And AngelScript code does NOT support the asterisk pointer (*), only the "@", which works basically the same?

And a new question:
When should I use SetArgObject and when SetArgAddress? Do they HAVE TO match the function declaration in the AS code (like Object for value types and Address for &in or @type)?

type& is allowed for reference types. It is equivalent to type&inout. If you set the engine property asEP_ALLOW_UNSAFE_REFERENCES like SiCrane mentioned, type& will also be allowed for value types and primitives, but as the name of the property suggests, this is unsafe and should not be used unless you don't care about the safety of your application.

Correct. AngelScript doesn't support the asterisk pointer (*). That tool is not safe in the hands of inexperienced script writers.

SetArgAddress can be used for both references and handles. For handles it will not automatically increment the object reference. SetArgObject can be used for handles, references to objects, and objects sent by value. For handles it will automatically increment the reference, and for objects sent by value it will make a copy of the object. Usually you'll want to use SetArgAddress for references, and SetArgObject for handles and objects sent by value.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632155
• Total Posts
3004473

×