初始化静态成员对象的最佳方法 [英] best way to initialize static member objects

查看:79
本文介绍了初始化静态成员对象的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个静态成员对象的类,一个是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.com>在消息中写道

news:ts ******************************** @ 4ax.com ...

我有两个静态成员对象的类,一个类型为int,另一个类型为vector< int> ;.

static int myStaticMemberInt
static vector< int> myStaticMemberVector;

我知道如何初始化int成员:

MyClass :: myStaticMemberInt = 99;

但是什么是initialzie的最佳方法myStaticMemberVector?

特别是,我想要使用的初始化代码有点复杂,并涉及处理一系列字符串以提出向量的每个int值。我很感激如何解决这个问题。

谢谢,
cpp



< blockquote>

" cppaddict" <他*** @ hello.com>在消息中写道

news:ts ******************************** @ 4ax.com ...

我有两个静态成员对象的类,一个类型为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.com> wrote in message
news:ts********************************@4ax.com...

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.com> wrote in message
news:ts********************************@4ax.com...

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


这篇关于初始化静态成员对象的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆