65,189
社区成员




问题二你是将构造函数私有化了啊,你可以参考一下singlton模式,
全局变量是在退出main函数以后析构的啊
:
class Test
{
public:
Test()
{
cout<<"Test"<<endl;
}
~Test()
{
cout<<"~Test"<<endl;
}
};
Test b;
int main()
{
Test a;
return 0;
}
#include<iostream>
#include<fstream>
using std::cin;
using std::cout;
using std::endl;
using std::ofstream;
class Monitor
{
public:
static int count;
static ofstream m_os;
public:
void incident()
{
++count;
}
void decrement()
{
--count;
}
void print()
{
if(m_os)
m_os<<Monitor::count <<endl;
}
};
int Monitor::count=0;
ofstream Monitor::m_os("D:\\01.txt",std::ios::app);
//-------------------
class Monitor2
{
void *d;
static ofstream m_os;
public:
Monitor2(Monitor *p)
{
d=p;
Monitor *s;
s=reinterpret_cast <Monitor *> (d);
s->incident();
s->print();
if(m_os)
m_os<<"Monitor2"<<endl;
}
~Monitor2()
{
if(m_os)
m_os<<"~Monitor2" <<endl;
reinterpret_cast <Monitor *> (d)->decrement();
reinterpret_cast <Monitor *> (d)->print();
}
};
ofstream Monitor2::m_os("D:\\02.txt",std::ios::app);
Monitor m1;
Monitor2 pp1(&m1);
int main()
{
return 0;
}
class A
{
A(int){}
A(const A &){}
static A s;
public:
static A* m()
{
return &s;
}
};
A A::s(3);
int main()
{
A *a=A::m();
return 0;
}