English 中文(简体)
安全转换(对物体的交换器)至(对固定物体的交换器)
原标题:Safely convert (vector of shared_ptr to objects) to (vector of shared_ptr to constant objects)
class A {};

typedef shared_ptr<const A*> AConstPtr;
typedef shared_ptr<A*> APtr;

vector<APtr> ptr;

const vector<AConstPtr>* foo()
{
    return &ptr;
}

This code does not compile, because "there is no implicit conversion from vector<Aptr>* to const vector<AConstPtr> * " Is there anyway to make this work, without creating a new vector, and without using an unsafe cast?

因此,我之所以需要,是因为我有一个在内部把清单作为矢量代码(<><Ptr>的班子,但需要通过接口使清单完全一致。

最佳回答

There s no way to do such a conversion since different shared_ptrs aren t related types.

First, are you really sure that you need to expose the implementation detail that there s an internal vector of shared pointers? That s really going to tie you to that implementation and it won t be subject to change without breaking API.

如何使用@Cubbi的建议,以及用<条码>取代<条码>的接口? 然后,你可以很容易地向外部客户代表一个集装箱,而不把 yourself打到<条码>>。

问题回答

暂无回答




相关问题
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?

热门标签