P
prawit.c
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.
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.