mancomb said:
Markus,
Thanks for your reply. The code in my original post was just pseudo
code intended to get some ideas of what I'm trying to do across. I
never expected that beast to compile anywhere but a parallel universe.
Please try to write minimal compiling samples for illustration. You'll
also get more answers faster and friendlier responses.
Yes the classes I'm dealing with are the shared pointers you mention.
Maybe I can use your clearer code to rephrase my question.
int main()
{
FooPtr foor(new Foo());
foor->doStuff();
return 0;
}
I want to pass my FooPtr into a function along with a pointer to the
function Foo::doStuff(). Then I would like to dereference my FooPtr
with the arrow operator and call the function doStuff through the
function pointer. If no FooPtr's were involved it would look like this:
// the function
void myFunc( Foo& myFoo, void Foo::*fooFunc() )
{
myFoo.*fooFunc();
}
// main:
int main()
{
Foo myFoo;
// assume Foo class has functions void doStuff(); and void
doMoreStuff();
myFunc(myFoo, &Foo::doStuff);
myFunc(myFoo, &Foo::doMoreStuff);
}
So I'd like myFunc to take in a FooPtr instead of just a Foo and still
keep it's functionality. Am I making sense yet or is it getting worse?
Both ;-) My codesample is already the solution to what you want to do,
really. You don't need a function 'myFunc'.
Assuming:
//------------------------------------------
class foo {
public:
void doStuff();
void doMoreStuff();
}
typedef boost::shared_ptr<foo> foo_ptr;
//------------------------------------------
Then you can call the methods doStuff() and doMoreStuff() via
derefencing a foo_ptr object:
//------------------------------------------
int main()
{
foo_ptr bar(new foo());
bar->doStuff(); // calls foo::doStuff with bar as this
bar->doMoreStuff();
return 0;
}
//------------------------------------------
You really do not need to write a call_stuff_through_foo_ptr() method;
to describe what happens behind the curtains:
Let's do:
foo* bar= new foo();
Now you could:
(*bar).doStuff();
Also written as:
bar->doStuff();
The operator-> is nothing special, here it is just syntactical sugar,
known from ancient C times. I am sure you know that you can call
methods this way. But what happens?
*bar
bar is of type 'pointer to foo', and variables of type 'pointer to ...'
can be dereferenced via the * operator; the result of applying the
dereference operator to a pointer is a ... (smile!) reference, in this
case, type 'reference to foo', also written as
foo&
Now, we are better, we use smart pointers. How they work is of no
interest to us in this stage. We just need to know that smart pointers
behave in almost all important cases like a raw pointer. This means
(with bar of type smart_ptr<foo>):
*bar
will be of type 'reference to foo'. Just like using a raw pointer. And
bar->doStuff()
will just call foo's member method doStuff(), applied on instance bar.
But maybe you meant applying non-member functions, which are only known
by signature and pointer, to an object. In this case, I recommend to
search the last week of this group for std:
stream and std::endl, where
an example of how to overload your operators to enable function
invocation is given.
best regards,
-- Markus