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;
}
相關文章
- vertx instance
- GPU InstanceGPU
- 3.2.1 Mounting a Database to an InstanceDatabase
- 3.1.5.6 Forcing an Instance to Start
- RMAN Duplicate RAC to Single Instance
- C++ 容器vector的使用C++
- linux c++ pprof的使用LinuxC++
- 前端每日一問--談談instance的原理前端
- 3.1.5.2 Starting an Instance, and Mounting and Opening a DatabaseDatabase
- Property [title] does not exist on this collection instance
- 【C/C++】c++多程式與hiredis的淺使用C++Redis
- C++純虛數的使用C++
- 使用C++的ORM框架QxORMC++ORM框架
- c++使用遇到的問題C++
- C++,std::shared_future的使用C++
- json的使用(python寫,c++讀)JSONPythonC++
- protobuf 的交叉編譯使用(C++)編譯C++
- Ruby class_eval and instance_eval notes
- LGWR (ospid: 29534): terminating the instance due to error 4021Error
- NSCFType unrecognized selector sent to instance等問題Zed
- TypeError: The ‘compilation‘ argument must be an instance of Compilation 報錯Error
- 進擊的 Vulkan 移動開發之 Instance & Device & Queue移動開發dev
- 使用 C++ WinRT 元件C++元件
- 使用 Visual C++ 的 Office 自動化C++
- c++模板類的使用,編譯的問題C++編譯
- ORA-15055 Unable to Connect to ASM When Starting an InstanceASM
- 【EmbedMask】《EmbedMask:Embedding Coupling for One-stage Instance Segmentation》Segmentation
- Oracle 19c Concepts(13):Oracle Database InstanceOracleDatabase
- Android C++層使用Binder通訊的方法AndroidC++
- C++中map的使用詳解說明C++
- 使用c++開發跨平臺的程式C++
- 【C++】 20_初始化列表的使用C++
- 【C++】智慧指標的正確使用方式C++指標
- C++ 抽象類快速使用C++抽象
- C++ | VS2017 C++專案配置使用的常見巨集定義C++
- 關於c++使用toml plusplus(俗稱toml++)的使用(4)C++TOML
- 關於c++使用toml plusplus(俗稱toml++)的使用(3)C++TOML
- How to Increase CSS Misscount in single instance ASM installations [ID 729878.1]CSSASM