单身人士的问题。 [英] Singleton question.
问题描述
所有
我写了一个程序来制作单件对象。
就像这样。
- --------------------------------------------------
A级{
公开:
静态A * getInstance();
void print(); \\打印出''a''我不会在这里实现。
void setA(int); \\'\设置''a'',我不会在这里实现。
~A(){}
受保护:
A(){}
private:
int a;
静态A *实例;
};
A * A :: instance = NULL;
A * A :: getInstance()
{
如果(实例)
返回实例;
else
return(instance = new A());
}
int main()
{
A * a = A :: getInstance ();
a-> setA(10);
a-> print();
删除a; //< - 这一行是一个问题。请阅读以下内容。
a-> setA(15); //< - 这条线应该崩溃,不是吗?
a-> print(); //< - 这条线应该崩溃,不是吗?
返回0;
}
- -------------------------------------------------- --------
这个程序的结果是:
#。/ SingleTon
10
15
#
------------------------------ ------------------------------
我的问题是:
为什么A类的实例仍然存在?
我的理解是我们已删除该对象(行''删除a;'')
提前感谢。
Prawit Chaivong。
>删除一个; //< - 这一行是一个问题。请阅读以下内容。
a-> setA(15); //< - 这条线应该崩溃,不是吗?
a-> print(); //< - 这条线应该崩溃,不是吗?
返回0;
}
------------- ----------------------------------------------
该计划的结果是:
#。/ SingleTon
10
15
---------------- --------------------------------------------
我的问题是:
为什么A类的实例仍然存在?
我的理解是我们已删除该对象(行''删除a;'')
这是未定义的行为。在删除对象后访问成员时,人们不能指望任何确定的东西。这是另一回事
,你不应该这样做!
问候,
Srini
pr******@gmail.com 写道:所有
我写了一个生成单件对象的程序。
就像这样。
----------------------------- ----------------------
A班{
公开:
静态A * getInstance();
void print(); \\打印出''a''我不会在这里实现。
void setA(int);设置''a'',我不会在这里实现。
~A(){}
受保护:
A(){}
私人:
int a;
静态A *实例;
};
A * A :: instance = NULL;
A * A :: getInstance()
{
if(instance)
return instance;
else
return(instance = new A());
}
int main()
{A / a = A :: getInstance();
a-> setA(10);
a- > print();
删除a; //< - 这一行是一个问题。请阅读以下内容。
a-> setA(15); //< - 这条线应该崩溃,不是吗?
a-> print(); //< - 这条线应该崩溃,不是吗?
返回0;
}
------------- ----------------------------------------------
该计划的结果是:
#。/ SingleTon
10
15
---------------- --------------------------------------------
我的问题是:
为什么A类的实例仍然存在?
我的理解是我们已删除该对象(行''删除a;'')
提前感谢。
Prawit Chaivong。
如果我阻止删除此对象,您认为怎么样?
将Dtor声明为受保护。
>如果我阻止删除此对象,您的想法是什么?
您可以返回静态引用而不是指向单独
对象的指针。
静态A& A :: getInstance(无效)
{
静态A实例;
返回实例;
}
但是这个方法会对多线程应用程序产生影响。
通过声明Dtor为受保护。
破坏者不得私有化!编译器不允许你这样做。
问候,
Srini
Hi, all
I''ve written a program to produce singleton object.
Like this.
---------------------------------------------------
class A{
public:
static A* getInstance();
void print(); \\ print out ''a'' I won''t implement here.
void setA(int); \\ set ''a'', I won''t implement here.
~A(){}
protected:
A(){}
private:
int a;
static A* instance;
};
A* A::instance = NULL;
A* A::getInstance()
{
if(instance)
return instance;
else
return (instance=new A());
}
int main()
{
A * a = A::getInstance();
a->setA(10);
a->print();
delete a; // <-- This line is a question. Read below.
a->setA(15); // <-- This line should crash, isn''t it?
a->print(); // <-- This line should crash, isn''t it?
return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#
------------------------------------------------------------
My question is:
"Why instance of class A still exist? "
My understanding is we''ve deleted that object (line ''delete a;'')
Thank in advance.
Prawit Chaivong.
> delete a; // <-- This line is a question. Read below.
a->setA(15); // <-- This line should crash, isn''t it?
a->print(); // <-- This line should crash, isn''t it?
return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#
------------------------------------------------------------
My question is:
"Why instance of class A still exist? "
My understanding is we''ve deleted that object (line ''delete a;'')
This is undefined behavior. One cannot expect anything definitive when
you access members after deleting an object. Its a different matter
that you _should not_ do that!
Regards,
Srini
pr******@gmail.com wrote: Hi, all
I''ve written a program to produce singleton object.
Like this.
---------------------------------------------------
class A{
public:
static A* getInstance();
void print(); \\ print out ''a'' I won''t implement here.
void setA(int); \\ set ''a'', I won''t implement here.
~A(){}
protected:
A(){}
private:
int a;
static A* instance;
};
A* A::instance = NULL;
A* A::getInstance()
{
if(instance)
return instance;
else
return (instance=new A());
}
int main()
{
A * a = A::getInstance();
a->setA(10);
a->print();
delete a; // <-- This line is a question. Read below.
a->setA(15); // <-- This line should crash, isn''t it?
a->print(); // <-- This line should crash, isn''t it?
return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#
------------------------------------------------------------
My question is:
"Why instance of class A still exist? "
My understanding is we''ve deleted that object (line ''delete a;'')
Thank in advance.
Prawit Chaivong.
What do you think if I prevent this object from deleting.
By declare Dtor as protected.
> What do you think if I prevent this object from deleting.
You can return a static reference instead of a pointer to the lone
object.
static A& A::getInstance(void)
{
static A instance;
return instance;
}
But this method will have implications in a multi-threaded application.
By declare Dtor as protected.
Destructors must NOT be made private! The compiler would not allow you
to do that.
Regards,
Srini
这篇关于单身人士的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!