初始化静态成员对象的最佳方法

编程入门 行业动态 更新时间:2024-10-26 08:28:47
本文介绍了初始化静态成员对象的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个静态成员对象的类,一个是int类型,一个是 ,类型为vector< int> ;. static int myStaticMemberInt 静态向量< int> myStaticMemberVector; 我知道如何初始化int成员: MyClass :: myStaticMemberInt = 99; 但是,初始化myStaticMemberVector的最佳方法是什么? 特别是,我想要使用的初始化代码有些复杂,并涉及到处理一系列字符串以得出向量的每个int值的。我很感激有关如何解决这个问题的任何想法。 谢谢, cpp

解决方案

Cpp, 你能把所有这些东西放到另一个静态对象中吗?执行你的 向量的特殊初始化?也就是说,在构造函数中 为 母亲。对象,你可以执行一些复杂的C ++来计算向量的初始值。 另外,你可以做类似的事情 静态向量< int> myStaticMemberVector = foobar(); 其中foobar是一个返回向量的函数< int>执行 的价值 必要设置。我在想foobar()将是一个自由函数或静态 函数 的类,但我猜它可能是一个对象成员函数。如果计算中涉及的字符串 在 将是 是foobar(字符串stringarray []) > 编译时间或者可以静态确定。 这种方法可能有一些障碍,例如, 的问题IO频道 用于正在打开并准备从外面阅读的节目,没有 了解更多 关于你的节目,我可以不要说。 dave " cppaddict" <他*** @ hello>在消息中写道 news:ts ******************************** @ 4ax ...

我有两个静态成员对象的类,一个类型为int,另一个类型为vector< int> ;. static int myStaticMemberInt static vector< int> myStaticMemberVector; 我知道如何初始化int成员: MyClass :: myStaticMemberInt = 99; 但是什么是initialzie的最佳方法myStaticMemberVector? 特别是,我想要使用的初始化代码有点复杂,并涉及处理一系列字符串以提出向量的每个int值。我很感激如何解决这个问题。 谢谢, cpp

" cppaddict" <他*** @ hello>在消息中写道 news:ts ******************************** @ 4ax ...

我有两个静态成员对象的类,一个类型为int,另一个类型为vector< int> ;. static int myStaticMemberInt static vector< int> myStaticMemberVector; 我知道如何初始化int成员: MyClass :: myStaticMemberInt = 99; 但是什么是initialzie的最佳方法myStaticMemberVector? 特别是,我想要使用的初始化代码有点复杂,并涉及处理一系列字符串以提出向量的每个int值。我很感激如何解决这个问题。 谢谢, cpp

这个可能是更好的方法(例如 如果你可以避免使用静态的话,那就不用了!)。但是一个函数如何返回初始化的向量,例如 typedef std :: vector< int> int_vector; extern int_vector init_vector(); static int_vector my_vector = init_vector(); 效率不高,因为有很多数据移动(除非编译器 足够聪明以便将其优化掉),它应该有效。如果这是一个问题, 则可能: extern int init_vector(int_vector& v); static int_vector my_vector; static int temp = init_vector(my_vector);

cppaddict发布:

我有两个静态成员对象的类,一个类型为int,另一个类型为vector< int> ;. static int myStaticMemberInt 静态向量< int> ; myStaticMemberVector; 我知道如何初始化int成员: MyClass :: myStaticMemberInt = 99; 但是什么是initialzie的最佳方法myStaticMemberVector? 特别是,我想要使用的初始化代码有点复杂,并涉及处理一系列字符串以提出向量的每个int值。我很感激如何解决这个问题。 谢谢, cpp

上课静态变量并给它一个构造函数!一种方式。 -JKop

I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the int member: MyClass::myStaticMemberInt = 99; But what is the best way to initialzie myStaticMemberVector? In particular, the initialization code I want to use is somewhat complex, and involves processing a series of strings to come up with each int value for the vector. I''d appreciate any ideas on how to solve this problem. Thanks, cpp

解决方案

Cpp, Can you put all this stuff into another static object which can perform your special initialization of the vector ? That is to say, in the constructor for the "mother" object, you can perform some complex C++ to compute the initial values of the vector. Alternatively, you could do something like static vector<int> myStaticMemberVector = foobar(); where foobar is a function returning a vector<int> value which performs your necessary setup. I was thinking foobar() would be a free function or static function of the class, but I guess it could be a object member function . A variation on this would be foobar( string stringarray[]) if the strings involved in the computation are known at compile time or can be statically determined. There might be a few snags in this approach, for instance, the question of the IO channels for the program being open and ready to read from the outside, without knowing a bit more about your program, I can''t say. dave "cppaddict" <he***@hello> wrote in message news:ts********************************@4ax...

I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the int member: MyClass::myStaticMemberInt = 99; But what is the best way to initialzie myStaticMemberVector? In particular, the initialization code I want to use is somewhat complex, and involves processing a series of strings to come up with each int value for the vector. I''d appreciate any ideas on how to solve this problem. Thanks, cpp

"cppaddict" <he***@hello> wrote in message news:ts********************************@4ax...

I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the int member: MyClass::myStaticMemberInt = 99; But what is the best way to initialzie myStaticMemberVector? In particular, the initialization code I want to use is somewhat complex, and involves processing a series of strings to come up with each int value for the vector. I''d appreciate any ideas on how to solve this problem. Thanks, cpp

This is just off the top of my head, there are probably better ways (such as not using statics if you can avoid it!). But what about a function returning the initialized vector, such as typedef std::vector<int> int_vector; extern int_vector init_vector(); static int_vector my_vector = init_vector(); Not efficient in that there is a lot of data movement (unless the compiler is smart enough to optimize it away), it should work. If that is a problem, then maybe: extern int init_vector(int_vector& v); static int_vector my_vector; static int temp = init_vector(my_vector);

cppaddict posted:

I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the int member: MyClass::myStaticMemberInt = 99; But what is the best way to initialzie myStaticMemberVector? In particular, the initialization code I want to use is somewhat complex, and involves processing a series of strings to come up with each int value for the vector. I''d appreciate any ideas on how to solve this problem. Thanks, cpp

Make a class out of the static variable and give it an constructor! One way. -JKop

更多推荐

初始化静态成员对象的最佳方法

本文发布于:2023-11-06 07:48:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   静态   对象   成员   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!