如何将私有指针返回为const的指针列表? [英] How to return a private pointer to a list of pointers as const?
问题描述
我有一个指向指针列表的指针,作为一个私有变量。我也有一个getter,它返回指向列表的指针。我需要保护它免受更改。
I have a pointer to a list of pointers, as a private variable. I also have a getter that returns the pointer to the list. I need to protect it from changes.
我找不到如何在此上使用reinterpret_cast或const_cast的方法。
I couldn't find how to use reinterpret_cast or const_cast on this.
class typeA{
shared_ptr<list<shared_ptr<typeB>>> l;
public:
shared_ptr<list<shared_ptr<const typeB>>> getList(){return (l);};
};
编译器返回:
error: could not convert ‘((typeA*)this)->typeA::x’ from ‘std::shared_ptr<std::__cxx11::list<std::shared_ptr<typeB> > >’ to ‘std::shared_ptr<std::__cxx11::list<std::shared_ptr<const typeB> > >’|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
似乎是 const shared_ptr< list< shared_ptr< typeB>>>>> ;
和 shared_ptr< const list< shared_ptr< typeB>>>
正常。
是否可以将 l
作为完整的const返回,例如:
Is it possible to do return l
as a complete const, like:
const shared_ptr<const list<shared_ptr<const typeB>>>
或至少类似于:
shared_ptr<list<shared_ptr<const typeB>>>
?
引用而不是指针是不是一个选择。将 l
声明为 shared_ptr< list< shared_ptr< const typeB>><
也是不想要的解决方案。
References instead of pointers is not an option. To declare l
as shared_ptr<list<shared_ptr<const typeB>>>
also is not a wanted solution.
编辑:不再有'int'。
no 'int' anymore.
似乎不太可能是我想要的,但是建议的解决方案很好。是的,复制指针是可以接受的。
It seems as it is not possible exactly what I wanted, but the suggested solutions are good. Yes, copying pointers is acceptable.
我的糟糕,我没有立即放上typeB。我知道引用相对于指针的优点,但是我希望有类似的解决方案。
My bad i didn't put typeB immediately. I am aware of some advantages of references over pointers, but I hoped there is some similar solution.
推荐答案
您可以创建一个新的原始列表中的 const int
列表并返回:
You can create a new list of const int
's from your original list and return that:
std::shared_ptr<std::list<std::shared_ptr<const int>>> getList(){
return std::make_shared<std::list<std::shared_ptr<const int>>>(l->begin(), l->end());
}
如果要防止其他人更改返回的列表,请使其
If you want to prevent people from making changes to the returned list, make it const too:
std::shared_ptr<const std::list<std::shared_ptr<const T>>> getList(){
return std::make_shared<const std::list<std::shared_ptr<const T>>>(l->cbegin(), l->cend());
}
此函数返回的共享指针不指向原始列表,而是指向
The shared pointer returned by this function does not point to the original list but to the newly created list.
另一种可能是提供迭代器,该迭代器在取消引用后将返回 const T&
(其中 T 是您实际存储的类型)。这样,您每次浏览时都无需复制整个列表。示例:
An alternative may be to provide iterators that, when dereferenced, returns const T&
(where T is the type you actually store). That way there will be no need to copy the whole list every time you want to go though it. Example:
#include <iostream>
#include <list>
#include <memory>
struct example {
int data;
example(int x) : data(x) {}
};
template <class T>
class typeA {
std::shared_ptr<std::list<std::shared_ptr<T>>> l = std::make_shared<std::list<std::shared_ptr<T>>>();
public:
template< class... Args >
void add( Args&&... args ) {
l->emplace_back(std::make_shared<T>(std::forward<Args>(args)...));
}
// a very basic iterator that can be extended as needed
struct const_iterator {
using uiterator = typename std::list<std::shared_ptr<T>>::const_iterator;
uiterator lit;
const_iterator(uiterator init) : lit(init) {}
const_iterator& operator++() { ++lit; return *this; }
const T& operator*() const { return *(*lit).get(); }
bool operator!=(const const_iterator& rhs) const { return lit != rhs.lit; }
};
const_iterator cbegin() const noexcept { return const_iterator(l->cbegin()); }
const_iterator cend() const noexcept { return const_iterator(l->cend()); }
auto begin() const noexcept { return cbegin(); }
auto end() const noexcept { return cend(); }
};
int main() {
typeA<example> apa;
apa.add(10);
apa.add(20);
apa.add(30);
for(auto& a : apa) {
// a.data = 5; // error: assignment of member ‘example::data’ in read-only object
std::cout << a.data << "\n";
}
}
这篇关于如何将私有指针返回为const的指针列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!