boost shared_from_this和多重继承 [英] boost shared_from_this and multiple inheritance
问题描述
我现在在使用boost shared_from_this
和多重继承时遇到一些麻烦。
如下:
-
类
A
实现某些功能,shared_from_this
-
类
B
应继承shared_from_this
-
类
$ c> D 我有一个例外(D
继承A
和B
(class D:public A,public B {}) c> c
weak_ptr
) -
从
D
类继承shared_from_this
不是我的选项
我不知道如何解决这个问题。
我使用Visual C ++ 2010。 p>
扩展Potatoswatter的解决方案,如果你可以改变A和B使用一个略微不同于enable_shared_from_this。代码使用标准库版本,但boost实现应该类似。下面的解释
#include< memory>
template< typename T>
struct enable_shared_from_this_virtual;
class enable_shared_from_this_virtual_base:public std :: enable_shared_from_this< enable_shared_from_this_virtual_base>
{
typedef std :: enable_shared_from_this< enable_shared_from_this_virtual_base> base_type;
template< typename T>
friend struct enable_shared_from_this_virtual;
std :: shared_ptr< enable_shared_from_this_virtual_base> shared_from_this()
{
return base_type :: shared_from_this();
}
std :: shared_ptr< enable_shared_from_this_virtual_base const> shared_from_this()const
{
return base_type :: shared_from_this();
}
};
template< typename T>
struct enable_shared_from_this_virtual:virtual enable_shared_from_this_virtual_base
{
typedef enable_shared_from_this_virtual_base base_type;
public:
std :: shared_ptr< T> shared_from_this()
{
std :: shared_ptr< T> result(base_type :: shared_from_this(),static_cast< T *>(this));
return result;
}
std :: shared_ptr< T const> shared_from_this()const
{
std :: shared_ptr< T const> result(base_type :: shared_from_this(),static_cast< T const *>(this));
return result;
}
};
所以,目的是 struct A
将从 enable_shared_from_this_virtual< A>
和公开继承struct B
将从 enable_shared_from_this_virtual< B& ;
。因为它们都共享相同的公共虚拟对象,所以在层次结构中只有一个 std :: enable_shared_from_this
。
当你调用类 shared_from_this
时,它会执行 enable_shared_from_this_virtual< T& *
到T *,并使用shared_ptr的别名构造函数,以便新的shared_ptr指向T *并与单个共享指针共享所有权。
使用顶部的朋友是为了防止任何人直接访问virtual base的shared_from_this()成员。他们必须通过 enable_shared_from_this_virtual< T>
提供的。
例如:
#include< iostream>
struct A:public enable_shared_from_this_virtual< A>
{
void foo()
{
shared_from_this() - > baz();
}
void baz()
{
std :: cout< __PRETTY_FUNCTION__<< std :: endl;
}
};
struct B:public enable_shared_from_this_virtual< B>
{
void bar()
{
shared_from_this() - > baz();
}
void baz()
{
std :: cout< __PRETTY_FUNCTION__<< std :: endl;
}
};
struct D:A,B {};
int main()
{
std :: shared_ptr< D> d(new D);
d-> foo();
d-> bar();
return 0;
}
I am currently having some troubles when using boost shared_from_this
and multiple inheritance.
The scenario can be described as follows:
Class
A
implements some functionality and should inherit fromshared_from_this
Class
B
implements another functionality and should inherit fromshared_from_this
Class
D
inherits functionalities fromA
andB
(class D : public A, public B {}
)When using some class
B
functionality from classD
I got an exception (weak_ptr
)To inherit
shared_from_this
from classD
is not an option for me
I am not sure about how to solve this.
Oh, I am using Visual C++ 2010.
Expanding on Potatoswatter's solution, if you can change A and B to use a something slightly different than enable_shared_from_this. The code uses the standard library versions, but the boost implementation should be similar. Explanation below
#include <memory>
template<typename T>
struct enable_shared_from_this_virtual;
class enable_shared_from_this_virtual_base : public std::enable_shared_from_this<enable_shared_from_this_virtual_base>
{
typedef std::enable_shared_from_this<enable_shared_from_this_virtual_base> base_type;
template<typename T>
friend struct enable_shared_from_this_virtual;
std::shared_ptr<enable_shared_from_this_virtual_base> shared_from_this()
{
return base_type::shared_from_this();
}
std::shared_ptr<enable_shared_from_this_virtual_base const> shared_from_this() const
{
return base_type::shared_from_this();
}
};
template<typename T>
struct enable_shared_from_this_virtual: virtual enable_shared_from_this_virtual_base
{
typedef enable_shared_from_this_virtual_base base_type;
public:
std::shared_ptr<T> shared_from_this()
{
std::shared_ptr<T> result(base_type::shared_from_this(), static_cast<T*>(this));
return result;
}
std::shared_ptr<T const> shared_from_this() const
{
std::shared_ptr<T const> result(base_type::shared_from_this(), static_cast<T const*>(this));
return result;
}
};
So, the intent is that struct A
would inherit publicly from enable_shared_from_this_virtual<A>
and struct B
would inherit publicly from enable_shared_from_this_virtual<B>
. Since they both share the same common virtual object, there is only one std::enable_shared_from_this
in the hierarchy.
When you call either classes shared_from_this
, it performs a cast from enable_shared_from_this_virtual<T>*
to T*, and uses the aliasing constructor of shared_ptr so that the new shared_ptr points to T* and shares ownership with the single shared pointer.
The use of the friend at the top is to prevent anyone from accessing the shared_from_this() members of the virtual base directly. They have to go through the ones provided by enable_shared_from_this_virtual<T>
.
An example:
#include <iostream>
struct A : public enable_shared_from_this_virtual<A>
{
void foo()
{
shared_from_this()->baz();
}
void baz()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
struct B : public enable_shared_from_this_virtual<B>
{
void bar()
{
shared_from_this()->baz();
}
void baz()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
struct D: A, B {};
int main()
{
std::shared_ptr<D> d(new D);
d->foo();
d->bar();
return 0;
}
这篇关于boost shared_from_this和多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!