好奇的单身人士模式? [英] curiosity singleton pattern?

查看:80
本文介绍了好奇的单身人士模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好组,


无法理解以下内容:


场景*场景:: sole(0); //在Scene.cpp中


类场景//在Scene.h中单例

{

public:

静态场景*获取()

{

//返回鞋底?鞋底:new(std :: nothrow)场景;

返回鞋底; //仅用于测试目的

}


int打印(无效)//仅限于有效对象?!

{

返回reinterpret_cast< unsigned int>(鞋底);

//其他值也可以(返回10;)

}


受保护:

场景();

~场景();


私人:< br $>
静态场景*鞋底;

};


int main(无效)

{

std :: cout<<场景:: Get()<< std :: endl;

std :: cout<<场景:: Get() - > Print()<< std :: endl;

}


返回


0

0


非静态函数打印由nullpointer调用?!有gcc 4.1和linux操作系统。


谢谢你的回答

真诚的你

chris

hello group,

cant understand the following:

Scene* Scene::sole(0); // in Scene.cpp

class Scene // singleton in Scene.h
{
public:
static Scene* Get()
{
//return sole ? sole : new (std::nothrow) Scene;
return sole; // for testing purposes only
}

int Print(void) // normaly only with valid objects?!
{
return reinterpret_cast<unsigned int>(sole);
// other values also possible (return 10;)
}

protected:
Scene();
~Scene();

private:
static Scene* sole;
};

int main(void)
{
std::cout << Scene::Get() << std::endl;
std::cout << Scene::Get()->Print() << std::endl;
}

returns

0
0

non static func Print is called by nullpointer?! have gcc 4.1 and linux os.

thanks for your answers
sincerely yours
chris

推荐答案

Chris Forone写道:
Chris Forone wrote:

你好组,


无法理解以下:


场景*场景::鞋底(0); //在Scene.cpp中


类场景//在Scene.h中单例

{

public:

静态场景*获取()

{

//返回鞋底? sole:new(std :: nothrow)场景;
hello group,

cant understand the following:

Scene* Scene::sole(0); // in Scene.cpp

class Scene // singleton in Scene.h
{
public:
static Scene* Get()
{
//return sole ? sole : new (std::nothrow) Scene;



为什么这一行被注释掉了?

Why is this line commented out?


返回鞋底; //仅用于测试目的

}


int Print(void)//仅限于有效对象?
return sole; // for testing purposes only
}

int Print(void) // normaly only with valid objects?!



我们不打扰C ++中的(void),只需使用()。

We don''t bother with the (void) in C++, just use ().


{

return reinterpret_cast< unsigned int>(sole);

//其他值也可以(返回10;)

}


受保护:

场景();

~场景();


私人:

静态场景*鞋底;

};


int main(无效)

{

std :: cout<<场景:: Get()<< std :: endl;

std :: cout<<场景:: Get() - > Print()<< std :: endl;

}


返回


0

0


非静态函数打印由nullpointer调用?!有gcc 4.1和linux操作系统。
{
return reinterpret_cast<unsigned int>(sole);
// other values also possible (return 10;)
}

protected:
Scene();
~Scene();

private:
static Scene* sole;
};

int main(void)
{
std::cout << Scene::Get() << std::endl;
std::cout << Scene::Get()->Print() << std::endl;
}

returns

0
0

non static func Print is called by nullpointer?! have gcc 4.1 and linux os.



为什么不呢?不要忘记,该对象并不习惯称成员

函数,这只是一个普通的旧C函数,带有额外的这个

参数,但是作为this参数传递给函数。在

你的情况下,这个没用过。


-

Ian Collins。

Why not? Don''t forget that the object isn''t used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn''t used.

--
Ian Collins.


Alf P. Steinbach schrieb:
Alf P. Steinbach schrieb:

你还有什么期望,为什么?
What else did you expect, and why?



某种运行时错误...可以访问非静态函数,无效

对象?!

some kind of runtime error... can access nonstatic func with NO VALID
object?!


您是否尝试过删除Scene :: Get中的out-commenting?
Have you tried, like, removing the out-commenting in Scene::Get?



当然,正常行为(有效对象的地址)

of course, normal behavior (address of valid object)


Ian Collins schrieb:
Ian Collins schrieb:

> //返回鞋底? sole:new(std :: nothrow)场景;
> //return sole ? sole : new (std::nothrow) Scene;



为什么这一行被注释掉了?


Why is this line commented out?



用于测试运算符new not successful

for testing operator new not successful


>
>

>返回鞋底; //仅用于测试目的

int Print(void)//仅限于有效对象?
> return sole; // for testing purposes only
}

int Print(void) // normaly only with valid objects?!



我们不打扰C ++中的(void),只需使用()。


We don''t bother with the (void) in C++, just use ().



抱歉...... ;-)

sorry... ;-)


> non static func print由nullpointer调用?!有gcc 4.1和linux操作系统。
>non static func Print is called by nullpointer?! have gcc 4.1 and linux os.



为什么不呢?不要忘记,该对象并不习惯称成员

函数,这只是一个普通的旧C函数,带有额外的这个

参数,但是作为this参数传递给函数。在

你的情况下,这个没用过。

Why not? Don''t forget that the object isn''t used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn''t used.



肯定吗?

sure?


这篇关于好奇的单身人士模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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