[英]Accessing an outer public member from an inner class
I have the following classes and I am trying to overload the operator* from the inner class iterator 我有以下类,并且我试图从内部类迭代器重载operator *
#ifndef __LISTMAP_H__
#define __LISTMAP_H__
#include "xless.h"
#include "xpair.h"
template <typename Key, typename Value, class Less=xless<Key>>
class listmap {
public:
using key_type = Key;
using mapped_type = Value;
using value_type = xpair<const key_type, mapped_type>;
private:
Less less;
struct node;
struct link {
node* next{};
node* prev{};
link (node* next, node* prev): next(next), prev(prev){}
};
struct node: link {
value_type value{};
node (node* next, node* prev, const value_type&);
};
node* anchor() { return static_cast<node*> (&anchor_); }
link anchor_ {anchor(), anchor()};
public:
class iterator;
listmap(){};
listmap (const listmap&) = default;
listmap& operator= (const listmap&) = default;
~listmap();
iterator insert (const value_type&);
iterator find (const key_type&);
iterator erase (iterator position);
iterator begin() { return anchor()->next; }
iterator end() { return anchor(); }
bool empty() const { return begin() == end(); }
};
template <typename Key, typename Value, class Less>
class listmap<Key,Value,Less>::iterator {
private:
friend class listmap<Key,Value>;
listmap<Key,Value,Less>::node* where {nullptr};
iterator (node* where): where(where){};
public:
iterator(){}
value_type& operator*();
value_type* operator->();
iterator& operator++(); //++itor
iterator& operator--(); //--itor
void erase();
bool operator== (const iterator&) const;
bool operator!= (const iterator&) const;
};
template <typename Key, typename Value, class Less>
value_type& listmap<Key,Value,Less>::iterator<Key,Value,Less>::operator*()
{
return where->value;
}
#include "listmap.tcc"
#endif
The problem is that value_type is a public member from the class listmap and it's not static, so I don't know how to complete the declaration of operator*(). 问题在于value_type是类listmap中的public成员,并且不是静态的,因此我不知道如何完成operator *()的声明。 I wouldn't like to fix the bug by changing the structure of the code.
我不想通过更改代码的结构来修复该错误。 Ex: making
例如:制作
using value_type = xpair<const key_type, mapped_type>;
Global. 全球。 I am just wondering if there is some other trick I can use to access value_type.
我只是想知道是否可以使用其他技巧来访问value_type。
....edit: I have no idea how the inner class recognizes value_type .... edit:我不知道内部类如何识别value_type
It's barely the same as for iterator, you just have to add typename
keyword 它与迭代器几乎不一样,只需要添加
typename
关键字
typename listmap<Key,Value,Less>::value_type
static
ness doesn't matter for a type. static
性与类型无关紧要。
The alias 1 inside iterator 迭代器内部的别名1
template <typename Key, typename Value, class Less>
class listmap<Key,Value,Less>::iterator {
...
using value_type = typename listmap<Key,Value,Less>::value_type;
};
allows you to write the definition more succinctly using auto suffix type: 允许您使用自动后缀类型更简洁地编写定义:
template <typename Key, typename Value, class Less>
auto listmap<Key,Value,Less>::iterator::operator*() -> value_type&
{
return where->value;
}
Careful: inner iterator
class is not template, only listmap
is: 注意:内部
iterator
类不是模板,只有listmap
是:
listmap<Key,Value,Less>::iterator<Key,Value,Less>::operator
// ~~~~~~~~~~~~~~~~ remove this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.