C++类的定义
定义一个类,只能有一个对象,如何定义这个类。请写下源代码,谢谢
[解决办法]
其实我也不是太了解,给你个链接吧,希望对你有帮助http://topic.csdn.net/t/20040822/11/3297993.html
[解决办法]
这个也是
http://blog.csdn.net/ljob2006/archive/2009/11/08/4787633.aspx
[解决办法]
- C/C++ code
//test.hclass Test{private: Test(){} ~Test(){} static Test* p;public: static Test* GetInstance(){ if(!p) p = new Test; return p; }};//test.cpp#include"test.h"Test* Test::p=0;
[解决办法]
- C/C++ code
//.hclass Singleton { friend Singleton& theSingleObject();public: // 接口使用 void func();private: Singleton() {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); };Singleton& theSingleObject();// .cppSingleton& theSingleObject(){ static Singleton sobject; return sobject;}// 使用thSingleObject().func();
[解决办法]
class student
{
public:
student(int a)
{
stu=a;
}
private:
int stu;
};
void main()
{
student one(3);
return 0;
}
这是用的类的构造函数做的,还有一种用成员函数
class student
{
public:
void studentno(int a)
{
stu=a;
}
private:
int stu;
};
void main()
{
student one;
one.studentno(3);
return 0;
}
[解决办法]