C++ instance的使用
1、test.h
#pragma once
#include <string>
class Student
{
public:
Student();
Student(int age, std::string name);
virtual ~Student();
static int create_instance();
static void destroy_instance();
static Student *get_instance()
{
return m_instance;
}
void set_student(int age, std::string name);
void show();
private:
int m_age;
std::string m_name;
private:
static Student* m_instance;
};
2、test.cpp
#include "test.h"
#include <iostream>
#include <string>
using namespace std;
Student *Student::m_instance = NULL;
Student::Student()
{
m_age = 0;
}
Student::Student(int age, std::string name) : m_age(age), m_name(name)
{
}
Student::~Student()
{
cout << "bye." << endl;
}
int Student::create_instance()
{
Student::m_instance = new Student;
if(NULL != m_instance)
return 0;
else
return -1;
}
void Student::destroy_instance()
{
if(m_instance)
{
delete m_instance;
m_instance = NULL;
}
}
void Student::show()
{
cout << "age:" << m_age << " name:" << m_name << endl;
}
void Student::set_student(int age, std::string name)
{
m_age = age;
m_name = name;
}
3、main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "test.h"
using namespace std;
int main(void)
{
Student::create_instance();
Student::get_instance()->set_student(18, "alex");
Student::get_instance()->show();
Student::get_instance()->destroy_instance();
return 0;
}
相關文章
- C++ 物件的使用C++物件
- C++ 容器vector的使用C++
- C++中字串的使用C++字串
- C++中extern的使用C++
- Oracle下的Databse,Instance,SchemasOracle
- database和instance的區別Database
- instance和clientclient
- Oracle database instanceOracleDatabase
- The Instance and the Database (285)Database
- STATUS OF ORACLE INSTANCEOracle
- 【C/C++】c++多程式與hiredis的淺使用C++Redis
- c++使用遇到的問題C++
- C++純虛數的使用C++
- 使用C++的ORM框架QxORMC++ORM框架
- 使用微軟的 C++ REST SDK微軟C++REST
- C++中std::allocator的使用C++
- C++定時器的使用C++定時器
- linux c++ pprof的使用LinuxC++
- c++ sizeof使用C++
- 理解Database和InstanceDatabase
- Monitoring an SAP instance
- BIEE Instance OracleBIPresentat DownOracle
- Overview of Instance and Crash RecoveryView
- Oracle instance解釋Oracle
- protobuf 的交叉編譯使用(C++)編譯C++
- C++ 中各種map的使用C++
- RAC中刪除特定instance的sessionSession
- 關於INSTANCE RECOVERY過程的理解
- 使用 C++ WinRT 元件C++元件
- C++中使用介面C++
- 3.2.1 Mounting a Database to an InstanceDatabase
- object is not an instance of declaring classObject
- Introduction to an Oracle Instance (284)Oracle
- Overview of Instance and Database Startup (289)ViewDatabase
- Restricted Mode of Instance Startup (291)REST
- C++,std::shared_future的使用C++
- C++中compare函式的使用C++函式
- 【轉】C++ map的基本操作和使用C++