数据共享+指向自身类型的指针+智能指针

实际项目遇到如下要求:

1、某几个类(A,B,C,....) 可以共享某个数据类;

2A,B,C,....这些类只使用数据类中感兴趣的数据成员;

3、只要数据类的数据更新, A,B,C,...类中相应的数据也可更新;

  

 

思考: 

1.继承、组合、指针都有数据分享的功能,但结合上述要求,选择指针实现;

2.指针:让多个指针指向数据类,实现数据的实时共享;

 

存在问题:多个指针指向一个类(目标),存在指针管理问题

 

涉及的C++知识:

1.指向自身类型的指针;

2.“智能指针思想,计数,增、减,零删除;

3.类静态成员和静态函数

  

程序代码:

//org.h
#ifndef _ORG_H
#define _ORG_H

#include<iostream>
#include<string>
using namespace  std;

class org
{
public:
	org();
	~org();

public://数据接口
	void set_name(string s);
	void set_number(int number);
	void set_grades(double grd);

	string get_name();
	int get_number();
	double get_grades();

private://数据
	string std_name;   //名字
	int    std_number; //学号
	double std_grades; //分数

public://关键
	static unsigned int InstanceRefNum;  //计数器
	static org* Instance;                //指向自身类型的指针
	static org* GetDef(void);            //增计数,内存分配,返回自指针
	static void DelDefault(void);        //撤销---减计数,删除
};

#endif
//org.cpp
#include "org.h"
org::org():std_name("xxx"),std_number(1),std_grades(0.1)
{ }
org::~org()
{ }

unsigned int org::InstanceRefNum = 0;  //计数初始化
org* org::Instance = NULL;             //自指针初始化

org* org::GetDef()
{
	++InstanceRefNum;               //计数
	if( Instance == NULL )
	{
            Instance = new org();      //初次时 内存分配
	}
	return  Instance;             //返回自指针
}


void org::DelDefault(void)
{
	--InstanceRefNum;              //撤销一次 减数
	if( InstanceRefNum == 0 && Instance != NULL)
	{
		delete Instance;                     //撤销到零 删除
		Instance = NULL;
		cout << "InstanceRefNum= " <<InstanceRefNum<<" all is over!"<<endl;
	}
}
void org::set_name(string s)
{
  std_name = s;
}
void org::set_number(int number)
{
  std_number = number;
}
void org::set_grades(double grd)
{ 
  std_grades = grd;
}
string org::get_name()
{
  return std_name;
}
int org::get_number()
{
  return std_number;
}
double org::get_grades()
{
  return std_grades;
}
//A.h
#ifndef _A_H
#define _A_H
#include<iostream>
#include<string>
#include "org.h"
using namespace  std;

class A
{
public:
	A();
	~A();
public:
//利用指向源数据类的指针 获取 数据
string get_name();
int get_number();
double get_grades();
private:
string std_name;
int    std_number;
double std_grades;
public:
   org* Aptr_org; //数据源类 指针

};
#endif
//A.cpp
#include "A.h"
A::A():std_name("xxx"),std_number(1),std_grades(0.1)
{
	Aptr_org=org::GetDef();  //对象创建  就得到数据源的自指针
}
A::~A()
{
	org::DelDefault();    //析构时,撤销
}
string A::get_name()
{   
	std_name = Aptr_org->get_name();  //利用指向源数据类的指针 获取 数据
	return std_name;
}
int A::get_number()
{
	std_number =Aptr_org->get_number();
	return std_number;
}
double A::get_grades()
{
	std_grades = Aptr_org->get_grades();
	return std_grades;
}


//B.h
#ifndef _B_H
#define _B_H
#include<iostream>
#include<string>
#include "org.h"
using namespace  std;
class B
{
public:
	B();
	~B();
public:
//利用指向源数据类的指针 获取 数据
string get_name();
int get_number();
double get_grades();
private:
string std_name;
int    std_number;
double std_grades;
public:
org* Bptr_org;
};
#endif
//B.cpp
#include "B.h"
B::B():std_name("xxx"),std_number(1),std_grades(0.1)
{
	Bptr_org=org::GetDef();
}
B::~B()
{
	org::DelDefault();
}
string B::get_name()
{   
	std_name = Bptr_org->get_name();
	return std_name;
}

int B::get_number()
{
	std_number =Bptr_org->get_number();
	return std_number;
}
double B::get_grades()
{
	std_grades = Bptr_org->get_grades();
	return std_grades;
}

//测试主程序
#include <iostream>
#include <string>


#include "org.h"
#include "A.h"
#include "B.h"

using namespace std;

int main()
{
  //原始数据
  org a;
  cout <<"name:"<<a.get_name()<<'\t'
	   <<"number:"<<a.get_number()<<'\t'
	  <<"grades:"<<a.get_grades()<<endl;


 //源数据 修改一次
  org *p;
  p = org::GetDef();
  p->set_name("张三");
  p->set_number(1001);
  p->set_grades(97.0);


  A sta;
  cout <<"name:"<<sta.get_name()<<'\t'
	  <<"number:"<<sta.get_number()<<'\t'
	  <<"grades:"<<sta.get_grades()<<endl;
  
  B stb;
  cout <<"name:"<<stb.get_name()<<'\t'
	  <<"number:"<<stb.get_number()<<'\t'
	  <<"grades:"<<stb.get_grades()<<endl;

//源数据 修改一次
  p->set_name("李四");
  p->set_number(1002);
  p->set_grades(67.0);

  cout <<"name:"<<sta.get_name()<<'\t'
	  <<"number:"<<sta.get_number()<<'\t'
	  <<"grades:"<<sta.get_grades()<<endl;

  cout <<"name:"<<stb.get_name()<<'\t'
	  <<"number:"<<stb.get_number()<<'\t'
	  <<"grades:"<<stb.get_grades()<<endl;


  org::DelDefault();

  return true;
}



结果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值