C ++:创建模板化的Shared< T>对象而不是shared_ptr< T>目的 [英] C++: Creating a templated Shared<T> object rather than a shared_ptr<T> object

查看:158
本文介绍了C ++:创建模板化的Shared< T>对象而不是shared_ptr< T>目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我以前的问题,我希望boost::shared_ptr<A>实际上是A(或者也许是A*)的子类,以便可以在将A*作为其参数的方法中使用.

Per my previous question, I wish that a boost::shared_ptr<A> was actually a subclass of A (or perhaps A*) so that it could be used in methods that took A* as their argument.

请考虑以下课程:

class A
{
public:
    A(int x) {mX = x;}
    virtual void setX(int x) {mX = x;}
    virtual int getX() const {return mX;}
private:
    int mX;
};

在上一个问题中,我建议创建一个SharedA对象来解决此问题,并且大概可以做到这一点.

In the previous question, I proposed the creation of a SharedA object to take care of this, and presumably it does.

class SharedA : public A
{
public:
    SharedA(A* a) : mImpl(a){}
    virtual void setX(int x) {mImpl->setX(x);}
    virtual int getX() const {return mImpl->getX();}
private:
    boost::shared_ptr<A> mImpl;
};

如果我可以创建一个模板类来为我处理所有这些工作,那将是Grrrrrrrreat的想法.

It would be Grrrrrrrreat thought, if I could create a template class to take care of all of this for me.

template <class T>
class Shared : public T
{
public:
    SharedT(T* t) : mImpl(t)
    {
    //What kind of crazy voodoo goes here?
    }

private:
    boost::shared_ptr<T> mImpl;
};

如果有这个(以及Shared<T>中的适当构造函数),那么我可以执行以下操作:

If I had this, (along with the proper constructors in Shared<T>), then I could do the following:

A* indestructo_A = new Shared<A>(new A(100));
A* indestructo_A_clone = new Shared<A>(indestructo_A);
delete indestructo_A
cout << "Indestructo-A back with a vengence!" << indestructo_A_clone.getX();

问题:

  1. 这有用吗?还是仅当您要处理特别糟糕的代码时才使用它的实用程序.例如:

  1. Is this useful? Or is its utility only in corner cases where you're dealing with particularly bad code. For instance:

void aFunctionYouHaveToUse(A * a) { /一些有用的算法,然后/
删除 }

void aFunctionYouHaveToUse(A* a) { /some useful algorithm and then/
delete a; }

是否可以构建这样的模板化类? (我想您需要反思,对吗?)如果可以构建它,怎么办?

Is it possible to build such a templated class? (I guess you need reflection, right?) If you can build it, how?

推荐答案

shared_ptr不允许显式强制转换为A *有一个非常充分的理由(比继承有更好的方法,但是无论如何,这都是不可能的) ). shared_ptr和其他智能指针的全部目的是提供一个小的封装对象,其唯一目的是拥有一个指针并确定何时以及如何删除它.

There's a really, darn good reason why shared_ptr does not allow explicit casting to A* (there are better ways of doing it than inheriting, which would be impossible anyway). The whole purpose of shared_ptr, and other smart pointers, are to provide a small, encapsulated object that's sole purpose is to own a pointer and to decide when and how to delete it.

如果该对象允许相同的指针不加思索地随意传递,那么它将根本无法实现其目的.每当您深入研究智能指针以获取内部的原始指针时,您就会违反其所有权语义,然后必须非常小心地避免做任何愚蠢的事情.智能指针使您可以通过强制调用来获取内部指针,而不是在您不小心将其错误地传递给错误类型的函数时默默地这样做.就像智能指针说的那样:嘿,您知道自己在做的事很危险,对吧?那么,好了,您走了."

If this object allowed that same pointer to just get passed around wily-nily, without thought, then it simply could not serve its purpose. Any time you dig into a smart pointer to get at the raw pointer inside you violate its ownership semantics and must then take very careful care not to go doing something stupid. Smart pointers make you think about this by forcing you to make a call to get at the pointer inside rather than just silently doing so whenever you accidentally pass it to the wrong kind of function. Think of it like the smart pointer saying, "Hey, you know what you're doing can be dangerous, right? OK then, here you go."

恕我直言,如果共享指针不允许访问其指针,那么Universe将是一个更好的地方.不幸的是,这不是这个Universe,也不能是这个Universe,因为有时您仍然需要将该东西传递给不使用智能指针的函数.因此,由于我们不生活在一个更好的世界中,我们的智能指针确实允许访问,因此它们并非荡然无存.

IMHO, the universe would be a better place if shared pointers didn't allow access to their pointers. Unfortunately, that's not this universe and can't be this universe because occasionally you still need to pass that thing into a function that doesn't use smart pointers. So, since we don't live in that better universe, OUR smart pointers do allow access, they're just not sluts about it.

这篇关于C ++:创建模板化的Shared&lt; T&gt;对象而不是shared_ptr&lt; T&gt;目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆