用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 ...
随机推荐
- 洛谷 P2355 团体操队形
P2355 团体操队形 题目背景 X中学要团体操比赛了哦.队形该怎样排呢? 题目描述 有n(n<=100000)个团体操队员编号分别为1~n,参加运动会开幕式的团体操表演.其基本队形(分连续队形 ...
- ArcGIS engine中Display类库 (局部刷新)
转自原文 ArcGIS engine中Display类库 (局部刷新) Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这个类库 ...
- ArcGIS 空间查询一例
ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = Polygon ;//设置用于筛选几 ...
- ArcGIS Engine中删除要素的几种方法总结
转自原文 ArcGIS Engine中删除要素的几种方法总结 /// <summary> /// 通过IFeature.Delete方法删除要素 /// </summary> ...
- C#定义变量
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- amaze ui和bootstrap有哪些差别?
amaze ui和bootstrap有哪些差别? 问题 我最近在学amaze ui,感觉如果单从功能性来看和bootstrap最大差别也就是扁平化,不过妹子ui号称对国产本土化支持更好,这个具体表现在 ...
- 深入理解Android(4)——理解Android中的JNI(下)
在前面文章中简单介绍了JNI,这一篇文章来简单看一下jni.h中定义的一些常用方法,来实现通过C++调用Android中的Java代码. 转载请说明出处:http://blog.csdn.net/da ...
- 基于Linux平台Softimage XSI 演示
2009年底上映的<阿凡达>是电影特效的巅峰之作,就在本月初上映的变形金刚3每次观看之后看得眼花缭乱总能让我热血沸腾,要是自己能做出那样的特效该多好,Linux下研究Maya已经有一段日 ...
- hdu 6170
dp: http://blog.csdn.net/qq_28954601/article/details/77484676 #include <bits/stdc++.h> #define ...
- 【例题 8-12 UVA-12627】Erratic Expansion
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 规律+递归题 f[k][i] k时刻前i行的红气球个数 i<=2^(k-1) f[k][i] = 2*f[k-1][i]; i ...