2

Please consider this example, how do we force implicit conversion in function whose second parameter is pointer to member function. casting explicitly in argument list of function is not what I want to achive wright now. Instead I would like that compiler somehow do that like it does with FIRST parameter...

struct Base
{
    virtual ~Base() = 0 {}
};

struct Derived : public Base
{
    void f(){}
};

typedef void(Base::*polymorph)();

// how do I force IMPLICIT conversion here: EDIT: (polymorph type work only for polymorph pointer type no conversion)
void func(Base* arg1, polymorph arg2) // void* arg2, (void*) arg2 etc...  dosn't work
{
    polymorph temp = reinterpret_cast<polymorph>(arg2); // to achive this
}

int main()
{
    Derived* test = new Derived;
    // first parameter work but another gives an error
    func(test, &Derived::f); // BY NOT CHANGING THIS!
    delete test;
    return 0;
}
selbie
  • 100,020
  • 15
  • 103
  • 173
codekiddy
  • 5,897
  • 9
  • 50
  • 80
  • 1
    I suspect it just can't be done. In func all you know (statically) that you are dealing with a Base object, and it would be unsafe to pretend that any Base object has that method. - Also, just because there is a pointer involved, it doesn't mean you have to use `new/delete`: `Derived test; func(&test, &Derived::f);` – visitor Jan 06 '12 at 11:22
  • The question is not clear enough. Please explain it. – ali_bahoo Jan 06 '12 at 11:56
  • I want to be able to pas a pointer to member method of what ever type to the function argument which takes pointer-to-member argument. in my example function should take "polimorph" type but it wont. – codekiddy Jan 06 '12 at 13:09
  • If that's what you want, just make it a template: template void func(Base*, void(T::*)());` But the question is still, what you are going to do with the function pointer. – UncleBens Jan 06 '12 at 15:14
  • Yeah, Template is good!, I've allready considered that. I was just currious if there is some other way. thanks. – codekiddy Jan 06 '12 at 15:18

1 Answers1

2

As clean as it gets. Code below. But I have no idea who's "this" pointer is going to be referenced when "temp" actually gets invoked.

typedef void(Base::*polymorph)(); 

void func(Base* arg1, polymorph arg2)
{ 
    polymorph temp = arg2;
} 


int main() 
{ 
    Derived* test = new Derived; 
    // first parameter work but another gives an error 
    func(test, static_cast<polymorph>(&Derived::f)); 
    delete test; 
    return 0; 
} 
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Thanks for effort selbie, but second parameter is being converted explicitliy using static_cast but not implicitly as I intended. I wanted to do that in function main without casting operator. instead casting operator should be used somewere else. but that isn't possible isn it ? – codekiddy Jan 10 '12 at 13:07
  • @codekiddy - It's for the same reasons you can't implicitly cast a pointer to a base class to a pointer to a derived class. Besides that, how is it possibly safe to invoke Derived::f via "temp"? What object is going to invoke temp and what is its type? If the answer is "an instance of Base", then it is a big leap to assume type safety. Let me ask this: "What are you really trying to anyway?" If someone on my team was attempting to something screwy with pointer to member functions, we'd probably whiteboard a better design together involving interfaces and virtual methods. – selbie Jan 10 '12 at 19:40
  • what ever I was trying doesn't metter any more cos I've give up. thanks for your time! – codekiddy Jan 10 '12 at 19:54