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 创建类的单例的更多相关文章

  1. Java基础 static限定符的使用 以及【 static实现的 singleton(单例)设计模式】

    static实现的 singleton(单例)设计模式 /** static实现的 singleton设计模式 , 使得一个类只能够创建一个static对象 */ 模板设计结构: package Co ...

  2. iOS创建安全的单例

    创建安全的单例 #import "Singleton.h" @implementation Singleton static Singleton* _instance = nil; ...

  3. Kotlin对象:仅一行代码就可创建安全的单例

    作者:Antonio Leiva 时间:Jun 20, 2017 原文链接:https://antonioleiva.com/objects-kotlin/ Kotlin对象是Android开发人员不 ...

  4. 适配器、工厂模式、线程池、线程组、互斥锁、Timer类、Runtime类、单例设计模式(二十四)

    1.多线程方法 * Thread 里面的俩个方法* 1.yield让出CPU,又称为礼让线程* 2.setPriority()设置线程的优先级 * 优先级最大是10,Thread.MAX_PRIORI ...

  5. eval、exec及元类、单例实现的5种方法

    eval内置函数 # eval内置函数的使用场景:#   1.执行字符串会得到相应的执行结果#   2.一般用于类型转化,该函数执行完有返回值,得到dict.list.tuple等​dic_str = ...

  6. day29 二十九、元类、单例

    一.eval.exec内置函数 1.eval函数 eval内置函数的使用场景: ①执行字符串会得到相应的执行结果 ②一般用于类型转换得到dict.list.tuple等 2.exec函数 exec应用 ...

  7. Python全栈开发之9、面向对象、元类以及单例

    前面一系列博文讲解的都是面向过程的编程,如今是时候来一波面向对象的讲解了 一.简介 面向对象编程是一种编程方式,使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” ...

  8. python基础--反射、元类、单例设计模式

    反射:reflect,反射指的是一个对象应该具备可以检测.修改.增加自身属性的能力,反射就是通过字符串操作属性 hasattr(对象,带查询的属性名称) 判断某个对象中是否存在某个属性 getattr ...

  9. 学好Spark/Kafka必须要掌握的Scala技术点(二)类、单例/伴生对象、继承和trait,模式匹配、样例类(case class)

    3. 类.对象.继承和trait 3.1 类 3.1.1 类的定义 Scala中,可以在类中定义类.以在函数中定义函数.可以在类中定义object:可以在函数中定义类,类成员的缺省访问级别是:publ ...

随机推荐

  1. HDU——T 1075 What Are You Talking About

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 Time Limit: 10000/5000 MS (Java/Others)    Memory Lim ...

  2. poj 1087 A Plug for UNIX(字符串编号建图)

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14862   Accepted: 5026 ...

  3. IOS开发人员经常使用的10个Xcode插件

    IOS开发人员经常使用的10个Xcode插件 申请达人,去除赞助商链接 一个合适的插件意味着它能够适应不同的开发环境,Sublime Text 和TextMate就是非常好的样例.你知道Xcode也支 ...

  4. android framework 02

    Android底层开发1.安装Ubuntu系统2.Ubuntu配置开发环境: sudo apt-get install git-core gnupg flex bison gperf zip sudo ...

  5. Onvif开发之客户端搜索篇

    关于ONVIF的广播,有客户端搜索和服务端发现的区别:客户端向固定的网段和固定的端口发送广播消息,服务端在对应的端口回复广播请求消息本文首先介绍客户端如何进行广播的已经对广播回复的信息的基本处理. 客 ...

  6. Apple iMac性能基准测试

    这里我要向大家介绍的一款苹果操作系统下的性能测试软件名叫GeekBench,是加拿大PrimateLabs公司出品. 下载地址:http://www.primatelabs.ca/geekbench/ ...

  7. groupadd---创建一个新的工作组

    groupadd命令   groupadd命令用于创建一个新的工作组,新工作组的信息将被添加到系统文件中. 语法 groupadd(选项)(参数) 选项 -g:指定新建工作组的id: -r:创建系统工 ...

  8. 威佐夫博奕(Wythoff Game)

    出现奇异局面,先取者必败,反之后拿者必败 奇异局面:(0,0) (1,2) (3,5) (4,7) (ak,bk) ak=bk-k,ak=k*(1+√5)/2: 代码实现(poj 1067): #in ...

  9. PHP+FastCGI+Nginx动态请求处理配置

    Nginx不支持对外部程序的调用,所以必须通过FastCGI接口实现对外部程序的调用从而实现对client动态页面请求的处理. CGI的英文全称为Common Gateway Interface(公共 ...

  10. mac自己定义tree命令

    编辑文件: vim ~/.bash_profile 在文件末尾追加: alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____| ...