C ++使用const函数调用指向成员的指针 [英] C++ Call pointer to member with a map from a const function

查看:222
本文介绍了C ++使用const函数调用指向成员的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向成员的指针的地图,声明为:

  std :: map< char,T *)(const T& const T&)> op_map; 

我在我的类的构造函数中直接用指针填充我的地图:

  op_map ['+'] =& Operand :: op_add; 

例如,op_add源代码为:

  T op_add(const T& a,const T& b){
return a + b;
}

而我想从const函数调用我的指针成员。这里是源代码:

  IOperand * res_int32(char op,const IOperand& rhs)const {
IOperand * res = const_cast< IOperand *>(& rhs);
Operand< int> * tmp = dynamic_cast< Operand< int> *>(res);
T res_calc =(this-> * op_map [op])(_ value,(T)tmp-> getValue());
}

但它让我总是一个错误:

  Operand.hpp:70:64:error:passing'const std :: map< char,double(Operand< double> ,const double&),std :: less< char>,std :: allocator< std :: pair< const char,double(Oper double& > >'as'this'argument'std :: map< _Key,_Tp,_Compare,_Alloc> :: mapped_type& std :: map< _Key,_Tp,_Compare,_Alloc> :: operator [](const key_type&)[with _Key = char,_Tp = double(Operand< double> :: *)(const double& const double& ,_Compare = std :: less< char>,_Alloc = std :: allocator< std :: pair< const char,double(Operand< double> :: *)(const double& const double& >,std :: map< _Key,_Tp,_Compare,_Alloc> :: mapped_type = double(Operand< double> :: *)(const double& const double&),std :: map <_Key,_Tp,_Compare ,_Alloc> :: key_type = char]'丢弃限定符[-fpermissive] 
Operand.hpp:70:64:error:从'const Operand< double> * const'到'Operand& [-fpermissive]

您有任何解决方案吗?



谢谢。

解决方案

operator [] t应用于 const 映射,因为如果未找到键,它将插入一个新的元素。



如果未找到密钥,则会抛出异常:



<$ p $ p> T res_calc =(this-> * op_map.at(op))(_ value,(T)tmp-> getValue());
^^^^^^

在C ++ 03中,使用 find

  map_type :: const_iterator found = op_map.find (op)。 
if(found!= op_map.end()){
T res_calc =(this-> *(found-> second))(_ value,(T)tmp-> getValue );
} else {
// handle error
}

还需要将映射中的成员函数的类型更改为

  T(Operand :: *)(const T & const T&)const 
^^^^^

const 成员函数中调用 this


I have a map of pointer to member declared as :

std::map<char, T (Operand::*)(const T &, const T &)> op_map;

I fill my map with pointer to member directly in the constructor of my class with :

op_map['+'] = &Operand::op_add;

For example, op_add source code is :

  T op_add(const T & a, const T & b) {
    return a + b;
  }

And I want to call my pointer to member from a const function. Here is the source code :

  IOperand *res_int32(char op, const IOperand & rhs) const {
    IOperand *res = const_cast<IOperand *>(&rhs);
    Operand<int> *tmp = dynamic_cast<Operand<int>*>(res);
    T res_calc = (this->*op_map[op])(_value, (T)tmp->getValue());
  }

But it makes me always an error :

Operand.hpp:70:64: error: passing ‘const std::map<char, double (Operand<double>::*)(const double&, const double&), std::less<char>, std::allocator<std::pair<const char, double (Operand<double>::*)(const double&, const double&)> > >’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char, _Tp = double (Operand<double>::*)(const double&, const double&), _Compare = std::less<char>, _Alloc = std::allocator<std::pair<const char, double (Operand<double>::*)(const double&, const double&)> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = double (Operand<double>::*)(const double&, const double&), std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]’ discards qualifiers [-fpermissive]
Operand.hpp:70:64: error: invalid conversion from ‘const Operand<double>* const’ to ‘Operand<double>*’ [-fpermissive]

Have you got any solution ?

Thank you.

解决方案

operator[] can't be applied to a const map, since it inserts a new element if the key is not found.

In C++11, there is an at function which throws an exception if the key is not found:

T res_calc = (this->*op_map.at(op))(_value, (T)tmp->getValue());
                           ^^^^^^^

In C++03, you'll need to use find:

map_type::const_iterator found = op_map.find(op);
if (found != op_map.end()) {
    T res_calc = (this->*(found->second))(_value, (T)tmp->getValue());
} else {
    // handle error
}

You'll also need to change the type of the member functions in the map to

T (Operand::*)(const T &, const T &) const
                                     ^^^^^

in order to call them on this from a const member function.

这篇关于C ++使用const函数调用指向成员的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆