简体   繁体   中英

Is there an equivalent in Structured Text for C++ templates?

I'm new to Structured Text programming and I'm currently working on TwinCat3.

I would like to specify array sizes inside my code without polluting the global namespace with additional constants. I think it would be useful, especially in creating a library.

I can't find a solution to this problem. In case there isn't one, can you tell me the best practice to solve this problem? The only solutions I see are:

  • use global variables
  • allocate in the heap

You can try something like this.

class <someclass>{
    public:
       static const int ARR_SIZE;
}
const int X::ARR_SIZE = <somevalue>;

since the variable is class scope, you wouldn't pollute the global namespace. alternatively you can create a namespace your own and define the static const variable inside of it such as

namespace <somename>{
 static const int ARR_SIZE = <somevalue>;
 // other definitions.
}

in this case the const variable read-only and accessible inside the namespace and can be accessed through::ARR_SIZE outside the namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM