Cast function pointer

I need to use a pointer to a member function that takes an argument from a base class that is used in other code. Well, I just want to do [something], as an example below. This code works fine, but I wonder if such a throw is always safe? I can not do dynamiceither statichere.

#include <cstdio>                                                   

class C
{                                                           
public:                                                             
        C () : c('c') {}                                            
        virtual ~C() {}                                             

        const char c;                                               
};                                                                  

class D : public C
{                                                
public:                                                             
        D () : d('d') {}                                            
        virtual ~D() {}                                             

        const char d;                                               
};                                                                  

class A 
{                                                           
public:                                                             
        A () {}                                                     
        virtual ~A() {}                                             

        void f( C& c ) { printf("%c\n",c.c); }                      
        void g( D& d ) { printf("%c %c\n",d.c,d.d); }               
};                                                                  

int main (int argc, char const* argv[])                             
{                                                                   
        void (A::*pf)( C& c ) = &A::f;                              
        void (A::*pg)( D& d ) = reinterpret_cast<void (A::*)(D&)>(&A::f);

        A a;                                                        
        C c;                                                        
        D d;                                                        

        (a.*pf)( c );                                               
        (a.*pg)( d );                                               

        return 0;                                                   
}                                                              
+5
source share
4 answers

What you are trying to do cannot be legal in C ++. C ++ does not support any co-variance or contra-variance for function parameter types, whether it is a member function or a free function.

- .

class A 
{                                                           
public:          
  ...                                                   
  void f( C& c ) { printf("%c\n",c.c); }                      
  void f_with_D( D& d ) { f(d); }
  ...
};          

-

void (A::*pg)( D& d ) = &A::f_with_D;

A a;
D d;                                                        
(a.*pg)( d );

a.f C d .

EDIT: , ( ). , , ,

class A 
{                                                           
public:          
  ...                                                   
  void f( C& c ) { printf("%c\n",c.c); }                      
  void f( D& d ) { f(static_cast<C&>(d)); }
  ...
};          

A::f(D&), .

+2

dynamic_cast. ( , , reinterpret_cast, ).

reinterpret_cast ( , ). , cast, undefined - .

+1

, .
-, dynamic_cast , - .
-, dynamic_cast reinterpret_cast C-style (, , ), :


, .

, "" --, .
-, , , , A::f, , , .

, , (D C), , . :

void f( C& c ) { printf("f(C& c): %c\n",c.c); }
void g( D& d ) { printf("g(D& d): %c\n",d.d); }

:

f (C & c): c
f (C & c): d

" ? D C!". , - . this, +0. C ( ):

struct C{
        C () : c('c') {}
        int i; // mean
        const char c;
};

: output:

f (C & c): c
f (C & c): ╠

, . C::c +4 (+0 + sizeof int), printf . D , printf . , - undefined .

, : , .:)

+1

reinterpret_cast, . (. ), , , D C. , , ( pg D , - D).

: , , undefined - , reinterpret_cast , , , . , .

0
source

All Articles