简体   繁体   English

为什么我不能将const指针插入多集?

[英]Why can't I insert a const pointer to a multiset?

According to the documentation for multiset for instance see http://www.cplusplus.com/reference/set/multiset/insert/ . 例如,根据multiset的文档,请参阅http://www.cplusplus.com/reference/set/multiset/insert/ It should be possible to insert a const value. 应该可以插入一个const值。 In my example the multiset is a collection of pointers, but when I try to insert a const pointer I get an error. 在我的例子中,multiset是一个指针集合,但是当我尝试插入一个const指针时,我得到一个错误。

template<typename Key>
struct node_key: public node_base {
     node_key(Key k): _key(k) {}
     virtual ~node_key() {}
     const Key& key() const { return _key;}
protected:
     Key _key;
     void _copy_key(node_key<Key> *n) const { n->_key=_key;}
};

template <typename Compare>
struct ptr_less_key {
    ptr_less_key() : _comp() {}
    virtual ~ptr_less_key() {}

    template <typename Pointer>
    bool operator()(const Pointer& a, const Pointer& b) const { return _comp(a->key(), b->key()); }
    Compare _comp;
};

int main() {
  typedef node_key<int>* keyp;
  std::multiset<keyp,ptr_less_key<std::less<int>>> x;
  node_key<int> k(5);
  const node_key<int> *p=&k;
  x.insert(p); //this fails
  return 0;
}

What you are currently doing: You are not trying to insert a const pointer, as you think you do, but a non-const pointer to a const element . 你目前在做什么:你不是试图插入一个const指针,就像你想的那样,而是一个指向const元素非const指针。

Change this 改变这个

const node_key<int> *p=&k;

to this 对此

node_key<int> *const p=&k;

to make the const keyword apply on the pointer rather than on what it points to. 使const关键字适用于指针而不是它指向的内容。

Given 特定

 struct node {
     void member();
     void const_member() const;
 };

consider the four declarations 考虑四个声明

 node*              pointer_to_node;
 const node*        pointer_to_const_node;
 node* const        const_pointer_to_node;
 const node* const  const_pointer_to_const_node;

There are two different aspects of const ness: that of the object node and that of the pointer. const有两个不同的方面:对象node和指针node The first two declare mutable pointers to either node or const node . 前两个声明指向nodeconst node可变指针。 A conversion from node* to const node* is allowed (and implicit), but not the other way around, as this would allow to modify a const node . 允许(和隐式)从node*const node*转换,但不是相反,因为这将允许修改const node

The second two declarations declare the respective pointers to be constant, ie these pointers cannot be modified (though the node pointed to by const_pointer_to_node can. 后两个声明声明各个指针是常量,即这些指针不能被修改(尽管const_pointer_to_node指向的node可以。

 pointer_to_node->member();                              // okay
 pointer_to_node->const_member();                        // okay
 pointer_to_node = new node;                             // okay
 pointer_to_node = const_pointer_to_node;                // okay
 pointer_to_node = pointer_to_const_node;                // ERROR

 pointer_to_const_node->member();                        // ERROR
 pointer_to_const_node->const_member();                  // okay
 pointer_to_const_node = new node;                       // okay
 pointer_to_const_node = pointer_to_node;                // okay
 pointer_to_const_node = const_pointer_to_node;          // okay
 pointer_to_const_node = const_pointer_to_const_node;    // okay

 const_pointer_to_node->member();                        // okay
 const_pointer_to_node->const_member();                  // okay
 const_pointer_to_node = new node;                       // ERROR
 const_pointer_to_node = const_pointer_to_node;          // ERROR
 const_pointer_to_node = pointer_to_const_node;          // ERROR

 const_pointer_to_const_node->member();                  // ERROR
 const_pointer_to_const_node->const_member();            // okay
 const_pointer_to_const_node = new node;                 // ERROR 
 const_pointer_to_const_node = const_pointer_to_node;    // ERROR 
 const_pointer_to_const_node = pointer_to_const_node;    // ERROR 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能映射:::指向const的指针? - Why can't map::find a pointer to a const? 为什么不能在之后设置 const 指针? - Why can't a const pointer be set afterwards? 为什么不能用另一个指向const char的指针初始化一个指针? - Why can't a pointer be initialized with another pointer to a const char? 当作为函数参数传递时,为什么不能更改类型为“ const int *”的指针的地址? - Why can't I change address of a pointer of type `const int *` when passed as function argument? 为什么我不能在赋值的右侧放置指向const的指针? - Why can't I put a pointer to const on right hand side of assignment? 为什么在通过指针编译时不能分配 const 初始化 - Why can't assign const initialize while compile by pointer 如果`this`不是const,为什么我不能修改它? - If `this` is not const, why can't I modify it? 无法将&#39;const pointer const&#39;传递给const ref - Can't pass 'const pointer const' to const ref 为什么“具有const成员的结构”类型的指针不能指向“具有非const成员的结构”? - Why can't a pointer of a “struct with const member” type point to a “struct with non-const member”? 为什么指向非const成员函数的指针不能指向const成员函数而相反? - Why pointer to non-const member function can't point const member function and opposite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM