ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

The Shared_ptr Class(memory header)

2022-02-08 22:03:12  阅读:192  来源: 互联网

标签:memory back header vector Shared shared data ptr StrBlob


Initialization

1.A default initialized smart pointer holds a null pointer.

shared_ptr<vector<string>> p;  // shared_ptr that can point at a vector of string.

2.Using make_shared function allocates and initializes an object in dynamic memeory and returns a shared ptr hat points to that object.

auto p = make_shared<vector<string>>(10,'9');  // P points to a string with value 9999999999.

3.Initializing a smart ptr from a pointer returned by new.

  • must use direct initialization.
  • can't implicitly convert a plain ptr to a smart ptr.
shared_ptr<int> p1 = new int(1024);  // error:must use direct initialization.(not copy initialization)
shared_ptr<int> p2(new int(1024));  // ok:uses direct initialization.

shared_ptr<int> clone(int p){
    return new in(p);    // error:implicit conversion to shared_ptr<int>.
}
shared_ptr<int> clone(int p){
    return shared_ptr<int>(new in(p));    // ok:explicitly create a shared_ptr<int> from int*.
}

Copying and Assigning

1.We can think of a shared_ptr as if it has an associated counter, usually referred to as a reference count.

Counter incremented when:

  • use it(shared_ptr) as the right-hand operand of an assignment;
  • pass it to or return it from a function by value.

Once a shared ptr's counter goes to zero, the shared_ptr automatically frees the object that it manages.
Memory is not freed until the last shared_ptr goes away.

Classes with resources that have dynamic lifetime

1.Each vector "owns" its own elememts. When we copy a vector, the elements in the original vector and in the copy are separate from one another.

vector<string> v1;
{  // new scope.
    vector<string> v2 = {"a", "an", "the"};
    v1 = v2;  // copies the elements from v2 into v1.
}  // v2 is destroyed, which destroys the elements in v2.
   // v1 has three elements, which are copies of the ones originally in v2.
  • The elements allocated by a vector exist only while the vector itself exists.

2.Use dynamic memory to allow multiple objects to share the same state(data).

  • To ensure that the elements continue to exist, we'll store the vector in dynamic memory.
StrBlob.h
#ifndef MY_STRBLOB_H
#define MY_STRBLOB_H
#include <vector>
#include <string>
#include <initializer_list>
#include <memory>
#include <stdexcept>

using namespace std;

class StrBlob {
public:
	typedef vector<string>::size_type size_type;
	StrBlob();
	StrBlob(initializer_list<string> il);  // initializer_list is a library type that represents
	                                       // an array of values of the specified type.
	size_type size() const { return data->size(); }
	bool empty() const { return data->empty(); }

	void push_back(const string& t) { data->push_back(t); }
	void pop_back();

	string& front();
	string& back();

private:
	shared_ptr<vector<string>> data;
	void check(size_type i, const string& msg) const;
};

StrBlob::StrBlob(): data(make_shared<vector<string>>()){}
StrBlob::StrBlob(initializer_list<string> il):
	data(make_shared<vector<string>>(il)){}

void StrBlob::check(size_type i, const string& msg) const {
	if (i >= data->size())
		throw out_of_range(msg);
}

string& StrBlob::front() {
	check(0, "front on empty StrBlob");
	return data->front();
}

string& StrBlob::back() {
	check(0, "back on empty StrBlob");
	return data->back();
}

void StrBlob::pop_back() {
	check(0, "pop_back on empty StrBlob");
	data->pop_back();
}
#endif // !MY_STRBLOB_H


StrBlob.cpp
#include <iostream>
#include "Strblob.h"

int main() {
	StrBlob b1;
	{
		StrBlob b2 = { "a","an","the" };
		b1 = b2;
		b2.push_back("about");
		cout << b2.size() << endl;
	}
	cout << b1.size() << endl;
	cout << b1.front() << " " << b1.back() << endl;

	StrBlob b3 = b1;
	cout << b3.front() << " " << b3.back() << endl;

	return 0;
}
// output:
// 4
// 4
// a about
// a about

标签:memory,back,header,vector,Shared,shared,data,ptr,StrBlob
来源: https://www.cnblogs.com/Dy2MoRaw/p/15873049.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有