c++ - Converting among pointers-to-member -


i know can't convert pointer-to-member pointer-to-non-member (e.g., void*), can convert among pointers-to-member same class? e.g.: class c , types t , u, can convert t c::* u c::*?

i want able map string names pointers-to-member class. example, given:

template<class classtype> struct mbr_map_traits {     typedef std::string mbr_name_type;     typedef void* classtype::*any_mbr_ptr;     typedef std::map<mbr_name_type,any_mbr_ptr> map_type; };  /**  * %mbr_map used map string arbitrary pointer-to-member of class.  * @tparam classtype class members map to.  */ template<class classtype> struct mbr_map : mbr_map_traits<classtype>::map_type {     typedef typename mbr_map_traits<classtype>::mbr_name_type mbr_name_type;      /**      * initalizes entry in map mape \a name pointer-to-member.      * @param name name map.      * @param p    pointer-to-member map to.      */     template<typename membertype>     void mbr_init( mbr_name_type const &name, membertype classtype::*p ) {         typedef typename mbr_map_traits<classtype>::any_mbr_ptr any_mbr_ptr;         (*this)[ name ] = reinterpret_cast<any_mbr_ptr>( p );      // ok?     }      /**      * sets value of class member name.      * @param c     class member set.      * @param name  name of class member set.      * @param value value set member to.      * @return true if \a name exists in map.      */     template<typename membertype>     bool mbr_set( classtype &c, mbr_name_type const &name, membertype const &value ) {         typedef typename mbr_map<classtype>::const_iterator const_iterator;         const_iterator const found = this->find( name );         if ( found != this->end() ) {             typedef membertype classtype::*mbr_ptr;             c.*reinterpret_cast<mbr_ptr>( found->second ) = value; // ok?             return true;         }         return false;     } }; 

and one-time initialization:

struct s {     std::string s;     int i;     bool b; };  void mbr_map_init( mbr_map<s> *m ) {     m->mbr_init( "string_mbr", &s::s );     m->mbr_init( "int_mbr", &s::i );     m->mbr_init( "bool_mbr", &s::b ); } 

i can this:

using namespace std;  int main() {     mbr_map<s> m;     mbr_map_init( &m );      s s;      m.mbr_set( s, "string_mbr", string( "hello" ) );     m.mbr_set( s, "int_mbr", 42 );     m.mbr_set( s, "bool_mbr", true );      cout << s.s << endl;     cout << s.i << endl;     cout << s.b << endl;     return 0; } 

and prints values set. legal?

(the reason want map parameter names , values read configuration file structure members.)

as long make cast original type before using pointer-to-member there no problem.

from http://en.cppreference.com/w/cpp/language/reinterpret_cast description of reinterpret_cast:

.... purely compiler directive instructs compiler treat sequence of bits (object representation) of expression if had type new_type.

only following conversions can done....

1) expression of integral, enumeration, pointer, or pointer-to-member type can converted own type. the resulting value same value of expression.

so cast pointer-to-member of type string pointer-to-member of type othertype , @ later point casting same pointer-to-member of type othertype pointer-to-member of type string , won't change original value of pointer-to-member.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -