读书人

C++高手大牛请进,该如何解决

发布时间: 2012-04-03 12:38:19 作者: rapoo

C++高手大牛请进
class test1
{
public:
test1(int n)
{
b = n;
}

private:
static test1 a;
int b;
};

test1 test1::a (2);

类里再定义一个静态的自己,在实际运用中有什么作用吗

windows核心编程里有这种用法



[解决办法]
okok
google Single模式 也有这用法 。。
[解决办法]
单间模式的一种实现吧. 通常会把构造器设定为私有的, 禁止类外程序实力化. 而提供一个instance 方法来获得实力. 如此, 在整个程序中, 这个类只能产生受限的个数的实例.
[解决办法]
稍微改了下, 让它更适合点.这里没有做析构, 不过我想,表达私有构造器后,如何实力化对象是足够了.

// test6.cpp
// vs2003
// cl -EHsc test6.cpp

class test1 {
public:
static test1 * instance();
private:
test1(int n) : b(n) { }
static test1 * a;
int b;
};

test1 * test1::a = 0;

test1 * test1::instance() {
if(!a)
a = new test1(100);
return a;
}

int main() {
test1::instance();
}

[解决办法]
同一时刻,游戏只能有一个地图

读书人网 >C++

热点推荐