N******K 发帖数: 10202 | 1 比如一个类 TestClass
class TestClass
{
int m_value;
std::vector m_DataArray;
// memeber function
}
可以改为:
struct TestClassMemberVarible
{
int m_value;
std::vector m_DataArray;
}
class TestClass
{
std::shared_ptr m_MemberVarible;
// memeber function
void Share(TestClass& InputClass)
}
如果所有的类都这么设计 可以不用写 share_ptr 和 new 直接用堆栈
真正的类成员都在heap 所谓的类只是一个壳子+shared_ptr+函数 这样永远不会形成
shared_ptr 循环引用
也可以建立 一个类的array 类似 存在array的referece
TestClass A;
TestClass B;
std::vector SharedClassArray(2);
SharedClassArray[0].Share(A);
SharedClassArray[1].Share(B); |
w******p 发帖数: 166 | 2 see Herb's way:
http:channel9.msdn.com/Events/GoingNative/2013/My-Favorite-Cpp-10-Liner
and see that over use of shared_ptr deteriorates to equivalence of mark-and-
sweep GC:
http:www.cs.virginia.edu/~cs415/reading/bacon-garbage.pdf
also shared_ptr new has mutex to protect [pointer+counter] operations so it'
s slower |
k**********g 发帖数: 989 | 3 http://c2.com/cgi/wiki?HandleBodyPattern
【在 N******K 的大作中提到】 : 比如一个类 TestClass : class TestClass : { : int m_value; : std::vector m_DataArray; : // memeber function : } : 可以改为: : struct TestClassMemberVarible : {
|
g*********e 发帖数: 14401 | 4 c++本来就是让你手动管理内存的,这个搞还不如写java |
N******K 发帖数: 10202 | 5 具体说说?
【在 g*********e 的大作中提到】 : c++本来就是让你手动管理内存的,这个搞还不如写java
|
N******K 发帖数: 10202 | |
N******K 发帖数: 10202 | 7 具体说说?
and-
it'
【在 w******p 的大作中提到】 : see Herb's way: : http:channel9.msdn.com/Events/GoingNative/2013/My-Favorite-Cpp-10-Liner : and see that over use of shared_ptr deteriorates to equivalence of mark-and- : sweep GC: : http:www.cs.virginia.edu/~cs415/reading/bacon-garbage.pdf : also shared_ptr new has mutex to protect [pointer+counter] operations so it' : s slower
|
k**********g 发帖数: 989 | 8
Basically it is doing what auto_ptr, unique_ptr and shared_ptr is doing
today.
However, the Handle-Body Idiom has a long history. Each software framework
implement it their own way, and combine it with framework-specific features
not found on the STL / C++11 smart pointers.
The user can only manipulate handles, and the handle doesn't directly own
the resource. Instead, the handle owns a reference-counted object (
implemented with an atomic variable) which then owns the actual resource.
When the user performs an assignment, as in the C++ assignment operator = ,
one of the three can happen:
(1) Copy = Share
(2) Copy = Move (taken away from the original handle and transferred to the
new handle)
(3) Copy = Copy-on-Write, activated by a pair of explicit calls to "
BeginWrite" and "EndWrite" (which would be used by a convenience RAII helper
to ensure timely release.)
【在 N******K 的大作中提到】 : 具体说说? : : and- : it'
|
N******K 发帖数: 10202 | 9 你觉得我这么搞 有什么问题么?
我觉得挺好的 类全都变成了壳子->handle
features
,
【在 k**********g 的大作中提到】 : : Basically it is doing what auto_ptr, unique_ptr and shared_ptr is doing : today. : However, the Handle-Body Idiom has a long history. Each software framework : implement it their own way, and combine it with framework-specific features : not found on the STL / C++11 smart pointers. : The user can only manipulate handles, and the handle doesn't directly own : the resource. Instead, the handle owns a reference-counted object ( : implemented with an atomic variable) which then owns the actual resource. : When the user performs an assignment, as in the C++ assignment operator = ,
|
N******K 发帖数: 10202 | 10 为啥要手动管理? shared_ptr 吃干饭的?
c++有栈概念 或者说{} 非常好 加上 shared_ptr 可以随时干掉不用的对象
【在 g*********e 的大作中提到】 : c++本来就是让你手动管理内存的,这个搞还不如写java
|
k**********g 发帖数: 989 | 11
没问题,很好很强大,我只是善意提醒一下这方法有名堂,别人已经用了很多年了。
【在 N******K 的大作中提到】 : 你觉得我这么搞 有什么问题么? : 我觉得挺好的 类全都变成了壳子->handle : : features : ,
|