简体   繁体   English

C++ 模板的结构化文本中是否有等效项?

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

I'm new to Structured Text programming and I'm currently working on TwinCat3.我是结构化文本编程的新手,目前正在研究 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.由于变量是 class scope,因此您不会污染全局命名空间。 alternatively you can create a namespace your own and define the static const variable inside of it such as或者,您可以创建自己的命名空间并在其中定义 static const 变量,例如

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.在这种情况下, const 变量在命名空间内只读且可访问,并且可以通过命名空间外的::ARR_SIZE 访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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