如何插入“const Obj”进入 std::list

发布于 2024-12-01 01:26:11 字数 393 浏览 3 评论 0原文

我写了一个函数

    void addAnything(T const &obj)
    {
    ...
    m_list.push_front(obj); // - line returns C2664 - list is initialized by std::list<T*> m_list
     ...
    }

,我的问题是从“T *const”中的“const T”转换。我需要将它插入到这个列表中... =/ 任何将元素插入 std::list 的方法都需要 'const T& x'。 有没有办法将 const 项插入到我的列表中并保留 addAnything() 的参数? 也许通过调整 m_list ?

感谢您的任何建议!

I wrote a function

    void addAnything(T const &obj)
    {
    ...
    m_list.push_front(obj); // - line returns C2664 - list is initialized by std::list<T*> m_list
     ...
    }

and my Problem is to convert from 'const T' in 'T *const'. And I need to insert it into this list... =/ Any method for inserting elements into a std::list requires 'const T& x'.
Is there any way to insert an const item into my list and keep the parameter of addAnything()?
Maybe by adapting m_list?

Thx for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

誰認得朕 2024-12-08 01:26:11

您有一个 const 对象,并且尝试将其插入到指向非 const 对象的非 const 指针的容器中。我假设您打算使用 &obj,但即使如此,这也行不通:如果 obj 是临时的怎么办?您需要将 obj 制作为 T&,或者制作 obj 的副本(可能使用 new )并推送该地址。

请注意,当您有一个 T* 容器时,并不总是清楚谁拥有其中的 T。除非您明确不希望容器取得所有权(在这种情况下,推送新内容是一个坏主意),否则您可能需要使用 boost::ptr_list< /代码>。

You have a const object, and you're trying to insert it into a container of non-const pointers to non-const objects. I'm going to assume you meant to use &obj, but even then, this isn't going to work: what if obj is a temporary? You'll need to either make obj a T&, or make a copy of obj (probably with new) and push the address of that.

Note that when you have a container of T*, it is not always clear who owns the Ts in it. Unless you explicitly don't want the container to take ownership (in that case, pushing newed things is a bad idea), you may want to use boost::ptr_list<T>.

哽咽笑 2024-12-08 01:26:11

试试这个:

m_list.push_front(new T(obj));

Try this:

m_list.push_front(new T(obj));
别把无礼当个性 2024-12-08 01:26:11

您的代码有两个问题:首先,当列表需要指针时,您正在推送对象引用;其次,您的列表需要一个非常量指针。您可以通过创建 obj 的副本或将列表的类型更改为 std::list来解决第二个问题。

// 1.
m_list.push_front(new T(obj)); // don't forget to delete it later

// 2.
std::list<const T*> m_list;
m_list.push_front(&obj);

请注意,使用第二种方法时,您必须确保 obj 在您将指向它的指针存储在 m_list 中时有效。

There are two problems with your code: first, you are pushing an object reference when the list expects a pointer; second, your list requires a non-const pointer. You can solve the second problem by either creating a copy of obj or changing the list's type to std::list<const T*>.

// 1.
m_list.push_front(new T(obj)); // don't forget to delete it later

// 2.
std::list<const T*> m_list;
m_list.push_front(&obj);

Note, that with the second method, you have to make sure that obj is valid for as long as you store a pointer to it in m_list.

仙女 2024-12-08 01:26:11

您知道 std::list吗?定义?当对象从列表中删除时,您应该手动删除该对象。
我不会替换 std::list与 std::list

Are you aware of std::list<T*> definition? Manually you should delete object when is removed from the list.
I not than replace std::list<T*> with std::list<T>.

箹锭⒈辈孓 2024-12-08 01:26:11
 m_list.push_front(obj); 
// - line returns C2664 - list is initialized by std::list<T*> m_list

错误消息告诉您 m_list 的类型为 std::list。这意味着,m_list 可以保存 T* 类型的元素。但是您正在推送类型为 T&obj。引用不是指针。

有什么方法可以将 const 项插入到我的列表中并保留 addAnything() 的参数吗?也许通过调整 m_list ?

std::list<const T> m_list;
void addAnything( const T& obj ){
     m_list.push_front(obj);

     // ...

}
 m_list.push_front(obj); 
// - line returns C2664 - list is initialized by std::list<T*> m_list

Error message complains you that the m_list is of type std::list<T*>. Meaning, m_list can hold elements of type T*. But you are pushing obj which is of type T&. References are not pointers.

Is there any way to insert an const item into my list and keep the parameter of addAnything()? Maybe by adapting m_list?

std::list<const T> m_list;
void addAnything( const T& obj ){
     m_list.push_front(obj);

     // ...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文