English 中文(简体)
• 如何允许非最先进的制造影机
原标题:How to allow non-const copy constructor for temporaries

我如何允许一个配有复印件的班级,该班的复印件用灯片复制?

其背景是:

I have a function that should return a list of pointers to objects that all inherit from Base, so I need something like vector<Base*>. Given that vector<auto_ptr> is not much of an option, I wanted to write a simple wrapper around vector<Base*> that deletes all elements in its destructor.

我面临以下问题:

My class has a copy constructor as follows:

auto_list(auto_list& rhs);

因此,我可以把点人名单抄送新案件,并在旧案件中予以澄清。

But obviously, that won t work with return values because temporaries don t bind to a non-const reference. Seeing that auto_ptr can be returned from functions, how did they implement it?

注:我可以使用C++11或加强,因此,移动语或独一无二的字体不是选择。

如果能够提供帮助,这是我迄今为止的法典:

template <typename T> class auto_list
{
private:

    vector<T*> pointers;

public:

    auto_list(vector<T*>& pointers)
    {
        this->pointers = pointers;
    }

    auto_list(auto_list& rhs)
    {
        this->pointers = rhs.pointers;
        rhs.pointers.clear();
    }

    ~auto_list()
    {
        for(typename vector<T*>::const_iterator it = this->pointers.begin(); it != this->pointers.end(); it++)
        {
            delete (*it);
        }
    }

    auto_list& operator=(auto_list& rhs)
    {
        this->pointers = rhs.pointers;
        rhs.pointers.clear();
    }

    vector<T*> get_pointers() const
    {
        return this->pointers;
    }
};
最佳回答

这一类别将令人困惑,就像auto_ptr本身一样,我敦促你使用更明智的智能点。 即使你确实有理由不使用波斯特(而对于我来说,我可以想不到为什么不使用),那么,如何使用<代码>:tr1:共享_ptr<>?

If you re determined to pursue this course, then auto_ptr solves the problem of initialising from a temporary by wrapping a reference in a class (auto_ptr_ref). The wrapper is created by a conversion function from *this (which is an lvalue, and therefore can be bound to a non-const reference), and can then be passed by value to a constructor.

你也可以做类似的事情:

template <typename T> class auto_list_ref
{
    friend class auto_list<T>;
    auto_list_ref(auto_list<T> & ref) : ref(ref) {}
    auto_list<T> & ref;
};

template <typename T> class auto_list
{
public:
    // Add a constructor and conversion operator as follows:

    auto_list(auto_list_ref<T> rhs)
    {
        this->pointers = rhs.ref.pointers;
        rhs.ref.pointers.clear();
    }

    operator auto_list_ref<T>() {return auto_list_ref<T>(*this);}
};

rel=“nofollow>> 这里是一次示威游行。

问题回答

之所以发明高价值参考资料,是因为无法在C++03中工作。 我说,从根本上说,彻底打破了。 你正在努力打击无法做的事情。 采用回报值或配给点标准。

You could declare pointers as mutable, thus allowing you to declare your copy ctor and assignment ops as taking const auto_list & and still call clear. You need to use the resulting class carefully, however, as any copy will clear the object copied from.

如果你刚刚就没有<条码>做工作的话,即:浏览器和带;auto_ptr<Base>>与你的汽车清单一起工作,我建议你完全放弃这一类别,并写上你自己的计票人,他们使用的是<条码>:vector。 如果你们都需要储存<代码>Base的共同物体,那么你甚至可以将其计算在内,因此该代码将低于你目前撰写的习惯清单。

如果做不到这一点,你的第二个最佳选择可能是采用在C++11之前所处理的标准unique_ptr<>。 页: 1 如果你决定这一点,就非常谨慎,能够随时获得神学权(这些会因无法想象的事物而打断)。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签