用static 创建类的单例
1.0 说明
通过函数调用new的static 类对象,由于static 对象只能初始化一次,由此构成单例运行。
2.0 直接代码
代码为windows,win32工程,因为只有一个文件,不上传工程了。直接贴。
// **.cpp : 定义控制台应用程序的入口点。
// /************************************************************************/
/* 通过函数,获取某类为静态类,从而保证单例运行。
* 通过测试,_test_single_和_test_single__2 通过函数get_single_index_class
* 函数来获取的静态对象的指针地址相同,因为对象为静态对象。
/************************************************************************/
//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h> class base
{
public:
virtual bool func() = 0;
virtual int get_b() = 0;
virtual void set_b(int){
printf("------------------------------this is base class set_b function;\n");
};
protected:
base(){}
};
class Derive :public base
{
public:
Derive& operator= (const Derive& rhs);
bool func();
int get_b();
void set_b(int b_){
printf("------------------------------this is derive class set_b function;\n");
b = b_;
}
void initial(){ b = -1;}
private:
int b; };
bool Derive::func()
{
return true;
}
int Derive::get_b()
{
return b;
} base* get_index_class()
{
return (base*)(new Derive());
} base* get_single_index_class()
{
static Derive* _derive_index = new Derive();
return (base*)(_derive_index);
} base* get_brand_new_single_index_class()
{
static Derive* _derive_index = new Derive();
_derive_index->initial();
return (base*)(_derive_index);
} int main(int argc, char* argv[])
{
/*-------------------------test new ---------------------------------------------*/
base* _test = get_index_class();
_test->set_b(12);
int b = _test->get_b();
printf("input data is : %d,\n",b);
//
base* _test_2 = get_index_class();
int b_2 = _test_2->get_b();
printf("get_b is : %d,\n",b_2); /*-------------------------test single new -----------------------------------------*/
base* _test_single_ = get_single_index_class();
_test_single_->set_b(13);
int b_single = _test_single_->get_b();
printf("single class input data is : %d,\n",b_single);
//
base* _test_single__2 = get_single_index_class();
int b_single_2 = _test_single__2->get_b();
printf("single get_b is : %d,\n",b_single_2); /*-------------------------test single brand-new -----------------------------------------*/
base* _test_brand_new_single_ = get_brand_new_single_index_class();
_test_brand_new_single_->set_b(13);
int b_brand_new_single = _test_brand_new_single_->get_b();
printf("brand_new single class input data is : %d,\n",b_brand_new_single);
//
base* _test_brand_new_single__2 = get_brand_new_single_index_class();
int b_brand_new_single_2 = _test_brand_new_single__2->get_b();
printf("brand_new single get_b is : %d,\n",b_brand_new_single_2); system("Pause");
return 0;
}
3.0 结果
自己留着用。
不足之处,多多指教。非常感谢!
用static 创建类的单例的更多相关文章
- Java基础 static限定符的使用 以及【 static实现的 singleton(单例)设计模式】
static实现的 singleton(单例)设计模式 /** static实现的 singleton设计模式 , 使得一个类只能够创建一个static对象 */ 模板设计结构: package Co ...
- iOS创建安全的单例
创建安全的单例 #import "Singleton.h" @implementation Singleton static Singleton* _instance = nil; ...
- Kotlin对象:仅一行代码就可创建安全的单例
作者:Antonio Leiva 时间:Jun 20, 2017 原文链接:https://antonioleiva.com/objects-kotlin/ Kotlin对象是Android开发人员不 ...
- 适配器、工厂模式、线程池、线程组、互斥锁、Timer类、Runtime类、单例设计模式(二十四)
1.多线程方法 * Thread 里面的俩个方法* 1.yield让出CPU,又称为礼让线程* 2.setPriority()设置线程的优先级 * 优先级最大是10,Thread.MAX_PRIORI ...
- eval、exec及元类、单例实现的5种方法
eval内置函数 # eval内置函数的使用场景:# 1.执行字符串会得到相应的执行结果# 2.一般用于类型转化,该函数执行完有返回值,得到dict.list.tuple等dic_str = ...
- day29 二十九、元类、单例
一.eval.exec内置函数 1.eval函数 eval内置函数的使用场景: ①执行字符串会得到相应的执行结果 ②一般用于类型转换得到dict.list.tuple等 2.exec函数 exec应用 ...
- Python全栈开发之9、面向对象、元类以及单例
前面一系列博文讲解的都是面向过程的编程,如今是时候来一波面向对象的讲解了 一.简介 面向对象编程是一种编程方式,使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” ...
- python基础--反射、元类、单例设计模式
反射:reflect,反射指的是一个对象应该具备可以检测.修改.增加自身属性的能力,反射就是通过字符串操作属性 hasattr(对象,带查询的属性名称) 判断某个对象中是否存在某个属性 getattr ...
- 学好Spark/Kafka必须要掌握的Scala技术点(二)类、单例/伴生对象、继承和trait,模式匹配、样例类(case class)
3. 类.对象.继承和trait 3.1 类 3.1.1 类的定义 Scala中,可以在类中定义类.以在函数中定义函数.可以在类中定义object:可以在函数中定义类,类成员的缺省访问级别是:publ ...
随机推荐
- android ActionBar的使用
Action Bar主要功能包括: 1. 显示选项菜单 2. 提供标签页的切换方式的导航功能,能够切换多个fragment. 3. 提供下拉的导航条目. 4. 提供交互式活动视图取 ...
- 【剑指offer】Q25:二叉树中和为某一值的路径
说明:最烦的就是看别人的博客,题解里直接上代码,一行分析都没有.只是这个题... class BTNode(): def __init__(self, val = -1): self.val = va ...
- C++模板类代码只能写在头文件?
这个问题,实际上我几年前就遇到了.最近写个模板类玩的时候,再次遇到. 当我非常仔细的将定义和实现分开,在头文件中保留了最少的依赖后,一切就绪.cpp单独编过.但是当使用的时候,就会报告所有的函 ...
- android 图片特效处理之锐化效果
这篇将讲到图片特效处理的锐化效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理: 一.简单算法:分别获取当前像素点和八个周围像素点的RGB值,先求出当前像素点的RGB值与八个像素点RGB值的 ...
- vue -- config index.js 配置文件详解
此文章介绍vue-cli脚手架config目录下index.js配置文件 此配置文件是用来定义开发环境和生产环境中所需要的参数 关于注释 当涉及到较复杂的解释我将通过标识的方式(如(1))将解释写到单 ...
- Sco Unixware 7.1.3企业版服务器安装视频教程
Sco Unixware 7.1.3企业版服务器安装视频教程 UnixWare 7.1.3是最先进的工业标准Intel和AMD处理器系统运行平台.UnixWare 7.1.3是关键业务解决方案的可靠基 ...
- go语言的一个gui 开源 项目 https://github.com/andlabs/ui
go语言的一个gui 开源 项目 https://github.com/andlabs/ui 1 安装 mingw-w64 链接地址: http://mingw-w64.sourceforge. ...
- shutdown---系统关机
shutdown命令用来系统关机命令.shutdown指令可以关闭所有程序,并依用户的需要,进行重新开机或关机的动作. 语法 shutdown(选项)(参数) 选项 -c:当执行“shutdown - ...
- logname---显示用户名称
logname命令用来显示用户名称.
- python之-字符编码
1.内存和硬盘都是用来存储的. CPU:速度快 硬盘:永久保存 2.文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就可以启动一个进程,是在内存中的,所以在编辑器编 ...