在blob.hpp中我们会看到protected成员变量:
protected:
shared_ptr<SyncedMemory> data_;
shared_ptr<SyncedMemory> diff_;
shared_ptr<SyncedMemory> shape_data_;
vector<int> shape_;
int count_;
int capacity_;
其中data_,diff_,shape_data_ 就是指向数据,梯度,和形状数据的shared_ptr指针。在这里顺便学习一下shared_ptr,主要参考http://blog.csdn.net/sndaxdrs/article/details/6175701。自己写了点代码,体会了一下。
注意,编译时:g++ shared_ptr.cpp -lboost_system
// shared_ptr.cpp
#include<boost/shared_ptr.hpp>
#include<iostream>
#include<vector>
using namespace std;
using boost::shared_ptr;
class inS
{
public:
inS(int i, int j) : m(i), n(j) {}
inS() {}
void printm() { cout << "m = " << m << endl; }
void printn() { cout << "n = " << n << endl; }
void setm(int i) { m = i; }
void setn(int j) { n = j; }
private:
int m;
int n;
};
class S
{
public:
S() {inS1.reset(new inS(0,0));
inS2.reset(new inS(0,0));}
void print_inS1() { cout << "print_inS1" << endl;
inS1->printm();
inS1->printn(); }
void print_inS2() { cout << "print_inS2" << endl;
inS2->printm();
inS2->printn(); }
void set_inS1(int i, int j) { inS1->setm(i);
inS1->setn(j); }
void set_inS2(int i, int j) { inS2->setm(i);
inS2->setn(j); }
void S1pointtoS2() { inS1 = inS2; }
void printcount() { cout << "inS1.count: " << inS1.use_count() << endl;
cout << "inS2.count: " << inS2.use_count() << endl; }
private:
shared_ptr<inS> inS1;
shared_ptr<in

1万+

被折叠的 条评论
为什么被折叠?



