[英]How can I create a static object member of class?
I am fairly new to c++, especially in its techniques. 我对c ++很新,特别是在它的技术方面。 My question is, how can I create a static object member of a class itself.
我的问题是,如何创建类本身的静态对象成员。 What I mean is I declared a static member object inside a class.
我的意思是我在类中声明了一个静态成员对象。 Example:
例:
CFoo:CFoo *pFoo[2] = {0};
class CFoo
{
public: static CFoo *pFoo[2];
public: CFoo(int a);
public: CFoo *getFoo();
};
Now the problem is, how can I create the pFoo, like I want to create two static object pFoo, 现在的问题是,如何创建pFoo,就像我想创建两个静态对象pFoo一样,
pFoo[0] = new CFoo(0);
pFoo[1] = new CFoo(1);
so that I can use the getFoo method to return one of the pFoo, like, 这样我就可以使用getFoo方法返回一个pFoo,比如
CFoo *getFoo()
{
return pFoo[0]; //or pFoo(1);
}
Thanks alot guys. 非常感谢你们。 Hope my questions are clear.
希望我的问题很清楚。
Thanks again in advance. 再次感谢您的提前。 -sasayins
-sasayins
Let's improve your code one step at a time. 让我们一步一步改进您的代码。 I'll explain what I'm doing at each step.
我将解释我在每一步所做的事情。
Step 1, this isn't Java. 第1步,这不是Java。 You don't need to specify public for every member.
您不需要为每个成员指定公共。 Everything after
public:
is public until you specify something else ( protected
or private
). public:
后的所有内容public:
在您指定其他内容( protected
或private
)之前是公开的。 I also moved the definition of pFoo
after the class. 我在
pFoo
也pFoo
的定义。 You can't define a variable before it's been declared. 在声明变量之前,您无法定义变量。
class CFoo
{
public:
static CFoo *pFoo[2];
CFoo(int a);
CFoo *getFoo();
};
CFoo* CFoo::pFoo[2] = {0};
Step 2, pFoo
probably shouldn't be public if you're going to have a getFoo
member function. 第2步,如果您要使用
getFoo
成员函数, pFoo
可能不应该是公共的。 Let's enforce the interface to the class instead of exposing the internal data. 让我们强制实现类的接口 ,而不是暴露内部数据。
class CFoo
{
public:
CFoo(int a);
CFoo *getFoo();
private:
static CFoo *pFoo[2];
};
CFoo* CFoo::pFoo[2] = {0};
Step 3, you can return by pointer without bothering to use new
. 第3步,你可以通过指针返回而不必费心去使用
new
。 I've written C++ code for many years, and I'd have to look up how you delete
the memory that was newed for a static member variable. 我已经编写了多年的C ++代码,我必须查找如何
delete
为静态成员变量新建的内存。 It's not worth the hassle to figure it out, so let's just allocate them on the stack. 找出它是不值得的麻烦,所以让我们把它们分配到堆栈上。 Also, let's return them by
const
pointer to prevent users from accidentally modifying the two static
CFoo objects. 另外,让我们通过
const
指针返回它们,以防止用户意外修改两个static
CFoo对象。
class CFoo
{
public:
CFoo(int a);
const CFoo *getFoo();
private:
static CFoo foos[2];
};
CFoo CFoo::foos[2] = {CFoo(0), CFoo(1)};
The implementation of getFoo
then becomes: 然后
getFoo
的实现变为:
const CFoo * CFoo::getFoo()
{
return &foos[0]; // or &foos[1]
}
IIRC, the static member foos
will be allocated the first time you create a CFoo
object. IIRC,静态成员
foos
将在您第一次创建CFoo
对象时分配。 So, this code... 那么,这段代码......
CFoo bar;
const CFoo *baz = bar.getFoo();
...is safe. ...是安全的。 The pointer named
baz
will point to the static member foos[0]
. 名为
baz
的指针将指向静态成员foos[0]
。
You don't need the pointers here. 你不需要这里的指针。 In fact, they're probably not a good idea.
事实上,他们可能不是一个好主意。
The following code works for me: 以下代码适用于我:
#include <iostream>
struct Foo {
static Foo const foos[];
Foo(int i) : i(i) { }
int i;
};
Foo const Foo::foos[2] = { Foo(1), Foo(2) };
using namespace std;
int main() {
cout << Foo::foos[0].i << endl;
}
(Notice that (to keep the demo simple) I've made all members public
, which is probably not what you want. It works just as well with private members.) (请注意(为了保持演示简单)我已将所有成员
public
,这可能不是您想要的。它与私有成员一样。)
To initialize your array, you should write this: 要初始化您的数组,您应该写这个:
CFoo* CFoo::pFoo [2] = { new CFoo(0), new CFoo(1) };
Do not free the memory allocated that way. 不要释放以这种方式分配的内存。
There are a few ways to do this. 有几种方法可以做到这一点。 One is just to keep doing as you're doing and initialize the pFoo array like this
一个就是继续做你正在做的事情并像这样初始化pFoo数组
// In source file (not header)
CFoo* CFoo::pFoo[2] = {new CFoo(1), new CFoo(2)};
But I would suggest wrapping them in an accessor like this: 但我建议将它们包装在这样的存取器中:
// In header
class CFoo
{
public:
CFoo(int a);
CFoo* getFoo1() const;
CFoo* getFoo2() const;
private:
static CFoo* getFoo(int n);
};
// In source file
CFoo::CFoo(int a)
{
// Constructor implementation
}
CFoo* CFoo::getFoo1() const
{
return getFoo(1);
}
CFoo* CFoo::getFoo2() const
{
return getFoo(2);
}
CFoo* CFoo::getFoo(int n)
{
static CFoo* pFoo[2] = {new Foo(1), new Foo(2)};
return pFoo[n];
}
The main reason is that this gets around the static initialization order fiasco. 主要原因是这绕过了静态初始化命令fiasco。 When you have a static variable in a class, it's somewhat nondeterministic when it is initialized, which is dangerous especially when the variable is public.
当你在一个类中有一个静态变量时,它在初始化时有点不确定,这是危险的,特别是当变量是公共的时。 Wrapping in an accessor means that the variable will be initialized when the accessor is first called, and so you'll know it is initialized before you try use it.
在访问器中进行包装意味着在首次调用访问器时将初始化变量,因此在尝试使用它之前,您将知道它已初始化。 Plus you get the benefit of lazy initialization if it is not used for a while or at all.
如果它暂时不使用或根本没有使用,你还可以获得延迟初始化的好处。
The code above is not thread-safe, but I doubt you're using threads here. 上面的代码不是线程安全的,但我怀疑你在这里使用线程。
Also, you should probably review your coding style. 此外,您应该检查您的编码风格。 Prefixing classes with C is a somewhat archaic practice, and putting "public:" before every public function is bizarre, you only need to write it once.
使用C前缀类是一种有点过时的做法,在每个公共函数之前放置“public:”是奇怪的,你只需要编写一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.