Jump to content
msd

Const in function params

Recommended Posts

Hello, 

 

If I want to set all params of Delphi function to be const (because of small amount of memory) do I need to define it as:

 

function FnName(const varA: string; const varB: integer; const varC: boolean = false): boolean;   OR    function FnName(const varA: string; varB: integer; varC: boolean = false): boolean; 

 

Thanks in advance...

 

Share this post


Link to post

The first, because in the latter, you are saying the first parameter is const but the others are going to get local copies.

 

Also, for strings, there is no memory issue here you are solving. I really doubt any memory issue can exist with integer or boolean parameters. If so, probably you need to look at some kind of data structure you pass by reference.

 

Strings are copy-on-write. In this case you are just telling the compiler not to allow setting a new value to the string in that function. You are just preventing the reference count from being incremented at the beginning of the function call, you are not changing how the compiler passes the string reference.

Edited by Brandon Staggs

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×