Pass by Value to Function Pointer

I

Immortal Nephi

Class A and Class B are different objects. How can pass by Value
choose either A object or B object in the global function. The
function pointer should be able to choose either A's function or B's
function to run.

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

void read_write();
};

class B
{
public:
B() {}
~B() {}

void read_write();
};

void run( void (*pF)() )
{
pF();
}

int main()
{
A a;
B b;

run( a.read_write() );
run( b.read_write() );

return 0;
} // end function main
 
S

SG

Class A and Class B are different objects.  

A class is not an object.
How can pass by Value choose either A object or B object in the
global function.  The function pointer should be able to choose
either A's function or B's function to run.

Sorry what?
class A {
public:
        void read_write();
};

class B {
public:
        void read_write();
};

void run( void (*pF)() )
{
        pF();
}

int main() {
        A a;
        B b;
        run( a.read_write() );
        run( b.read_write() );
        return 0;
}

Of course, this won't compile. "a.read_write()" invokes a member
function on "a". This member function returns void and not a function
pointer.

If you want to do what I think you want to do you have several
options:

template<class C>
void run( C & obj, void (C::*pmf)() )
{
(obj.*pmf)();
}

int main() {
A a;
B b;
run( a, &A::read_write );
run( b, &B::read_write );
return 0;
}

Or you could use the "glorified function pointer" boost::function in
combination with boost::bind.

void run(boost::function<void()> func)
{
func();
}

int main() {
A a;
B b;
run( boost::bind(&A::read_write,&a) );
run( boost::bind(&B::read_write,&b) );
return 0;
}

I didn't test these examples.

Get a decent C++ book that explains pointers and pointers to members
and read it.

Cheers!
SG
 
I

Immortal Nephi

A class is not an object.


Sorry what?









Of course, this won't compile. "a.read_write()" invokes a member
function on "a". This member function returns void and not a function
pointer.

If you want to do what I think you want to do you have several
options:

  template<class C>
  void run( C & obj, void (C::*pmf)() )
  {
    (obj.*pmf)();
  }

  int main() {
    A a;
    B b;
    run( a, &A::read_write );
    run( b, &B::read_write );
    return 0;
  }

Or you could use the "glorified function pointer" boost::function in
combination with boost::bind.

  void run(boost::function<void()> func)
  {
    func();
  }

  int main() {
    A a;
    B b;
    run( boost::bind(&A::read_write,&a) );
    run( boost::bind(&B::read_write,&b) );
    return 0;
  }

I didn't test these examples.

Get a decent C++ book that explains pointers and pointers to members
and read it.

Cheers!
SG- Hide quoted text -

- Show quoted text -

Template code looks OK. I try to create static library. Put
function pointer as pass by value on the global function's parameter.
The global function is imported from the static library into main(),
but it does not look good idea. I consider another option.

// A_LIB BEGIN

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

friend void read_write(); // Declare read_write()
void run()
{
read_write();
}
};

// A_LIB END

// main.cpp

void read_write() {} // define read_write()


int main()
{
A a;

a.run();

return 0;
} // end function main

I don't know if C++ Compiler will compile A_LIB into static library
successfully because it does not have read_write() definition. Then,
you can define read_write() in your own on main.cpp and C++ Compiler
will compile to import A_LIB into main.cpp successfully.

Please provide your opinion. It is supposed to be good design.
 
S

SG

Please provide your opinion.  It is supposed to be good design.

You should mention what it is that you're trying to do. Otherwise we
can hardly suggest a better design.

If class A and B really have nothing in common (not even an abstract
base class with a virtual read_write, *hint hint*) and you don't want
"run" to be a function template then I think the only solution is to
use something like boost::function.

Cheers!
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top