用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 ...
随机推荐
- C# Arcgis Engine 获得鼠标按下位置的要素
public IFeature GetFeatureOnMouseDown(IPoint point) { try { ILayer layer = Common.GetLayerByName(mMa ...
- java导入大量Excel时报错
在项目中同事遇到一问题,如今给大家分享一下. 在程序里面导入两千多条数据后.程序就报错. 刚開始以为是内存的问题.在经过细致跟踪代码后发现每次都是833行的第三列报错.也就是第一万列.最后在网上找到了 ...
- 说说第二次配置Ubuntu14.04
任务下达.要装几台linux电脑.并配置能远程--事实上一開始我是拒绝的,内心里百般不想去做.由于干过一次.知道这活儿非常麻烦,这次又有新需求.技术上有非常多还不会.须要花费时间查资料.当时大概预计了 ...
- RvmTranslator6.5 is released
RvmTranslator6.5 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...
- sdut 2805(最小生成树)
大家快来A水题 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 (1<= N <=2000)(1<= M <= N*(N-1)/2 ...
- modSecurity规则学习(二)——配置文件
crs-setup.cnf #SecDefaultAction指令后的规则都继承这一设置,除非为某条规则指定了一个特定的动作,或者指定了新的SecDefaultAction.特别注意到,未经处理的di ...
- python-打开网页
最近一直想通过python来实现网页的操作.因为想把自己vimrc的配置直接通过脚本来实现.比较理想的情况下,就是通过一个脚本,把自己需要的一些资源直接从网上下载下来,然后再对下载的资源进行安装等操作 ...
- es6 -- 透彻掌握Promise的使用,读这篇就够了
Promise的重要性我认为我没有必要多讲,概括起来说就是必须得掌握,而且还要掌握透彻.这篇文章的开头,主要跟大家分析一下,为什么会有Promise出现. 在实际的使用当中,有非常多的应用场景我们不能 ...
- OpenStack安装及监控配置视频教程
OpenStack是一个美国国家航空航天局和Rackspace合作研发的云端运算软件,是一个自由软件和开放源代码项目,用来建立公共和私有云本,其软件使用比较复杂,本视频将讲解其安装和部分配置方法,以后 ...
- Stacked Autoencoders
转自:http://www.cnblogs.com/tornadomeet/archive/2013/03/25/2980357.html 如果使用多层神经网络的话,那么将可以得到对输入更复杂的函数表 ...