Syntax question function pointers

P

Patrick Kowalzick

Dear all,

I have the following code:

// ************************
class A {};
class B : public A {};

void foo( const B & ) {}

int main()
{
typedef void(* T_A_ptr)( const A & );

void(* B_ptr)( const B & ) = foo;
T_A_ptr B_Base_ptr = T_A_ptr( B_ptr );
}
// ************************

and want to simplify the last to lines ( no, not the } ). I want to press it
in one line. Has anybody an idea? The function pointer syntax is quite
confusing :).

Regards,
Patrick
 
R

Rolf Magnus

Patrick said:
Dear all,

I have the following code:

// ************************
class A {};
class B : public A {};

void foo( const B & ) {}

int main()
{
typedef void(* T_A_ptr)( const A & );

void(* B_ptr)( const B & ) = foo;
T_A_ptr B_Base_ptr = T_A_ptr( B_ptr );
}
// ************************

and want to simplify the last to lines ( no, not the } ). I want to press
it in one line. Has anybody an idea? The function pointer syntax is quite
confusing :).

You shouldn't do that in the first place. Your cast is not safe, since you
could afterwards pass an A to a function that actually expects a B.
 
J

JKop

The function pointer syntax
is quite confusing :).

I actually find your naming system more confusing.

Function pointer syntax is dead easy once you've gotten the hang of it.
Anyway...

class Base {};
class Derived : public Base {};

void ActualFunctionThatTakesDerived( Derived const & ) {}

int main()
{
typedef void (*p_FunctionThatTakesBase)( Base const & );
typedef void (*p_FunctionThatTakesDerived)( Derived const & );

p_FunctionThatTakesDerived p_FuncDerived =
ActualFunctionThatTakesDerived;

p_FunctionThatTakesBase p_FuncBase = reinterpret_cast
<p_FunctionThatTakesBase>( p_FuncDerived );

Derived derived_object;

p_FuncBase( derived_object );

}


You're passing by const reference, so your code is legal.

But... if you passed by value, the object would be sliced down to just a
Base object. The function would think it's working with a full Derived
object and when it goes to access the Derived part... undefined behaviour.

-JKop
 
P

Patrick Kowalzick

Dear Rolf,
You shouldn't do that in the first place. Your cast is not safe, since you
could afterwards pass an A to a function that actually expects a B.

the real code is for a dispatcher, more or less hidden for the "user".

There I created a table with function pointers to the base type, but to fill
in the right pointers I need the casting. Any other solution is appreciated.

Regards,
Patrick
 
P

Patrick Kowalzick

Hi JKop,
I actually find your naming system more confusing.

Question of taste.
Function pointer syntax is dead easy once you've gotten the hang of it.
Anyway...

Me personally I am quite far away to understand it.
Here I hang (see comments please):

// **********************
class A {};
class B : public A {};

void foo( const B & ) {}

int main()
{
typedef void(* T_A_ptr)( const A & );
typedef void(* T_B_ptr)( const B & );

T_A_ptr B_Base_ptr = reinterpret_cast<T_A_ptr>( T_B_ptr ( foo ) ); // if
this works ....
T_A_ptr B_Base_ptr = reinterpret_cast<T_A_ptr>( ( void(*)( const B & ) )
( foo ) ); // ...why does this here not work ?

}
// **********************

to use the reinterpret_cast is a good idea to keep it semantically easier to
understand.

Regards,
Patrick
 
J

JKop

Me personally I am quite far away to understand it.
Here I hang (see comments please):

Pick a name. Let's pick "SuperDuperFunction".

Now, turn:

SuperDuperFunction

into:

(*SuperDuperFunction)


Now proceed as normal:

void (*SuperDuperFunction)(int);


Similarly for declaring a reference to an array:

Pick the reference's name, let's say "poo".

Turn

poo

into:

(&poo)

And proceed as normal:

int (&poo)[5] =

to use the reinterpret_cast is a good idea to keep it semantically
easier to understand.

I'm not sure if you can convert:

void (*)(Derived const &)

to

void (*)(Base const &)

implicitly.

I could've simply just whipped out my compiler and tested
it. I didn't bother. Instead I used brute force as I knew
it would work, reinterpret_cast will convert any type of
pointer to any type of pointer for you.


-JKop
 
P

Patrick Kowalzick

Me personally I am quite far away to understand it.
Here I hang (see comments please):

Pick a name. Let's pick "SuperDuperFunction".

Now, turn:

SuperDuperFunction

into:

(*SuperDuperFunction)


Now proceed as normal:

void (*SuperDuperFunction)(int);


Similarly for declaring a reference to an array:

Pick the reference's name, let's say "poo".

Turn

poo

into:

(&poo)

And proceed as normal:

int (&poo)[5] =

Hmm, that does not really help. I _know_ these cases and I can _use_ them.
Anyway I do not _understand_ the concept of the syntax. And IMO this is the
reason why I could not use function pointers as effective as I'd like to.

Thanks,
Patrick
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top