473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Syntax question function pointers

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
Jul 22 '05 #1
6 1262
Patrick Kowalzick wrote:
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.
Jul 22 '05 #2
>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

Jul 22 '05 #3
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
Jul 22 '05 #4
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
Jul 22 '05 #5
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):


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
Jul 22 '05 #6
> > 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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aaron Walker | last post by:
Greetings, I'm attempting to write my first *real* template function that also deals with a map of strings to member function pointers that is making the syntax a little tricky to get right. ...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
5
by: damian birchler | last post by:
What's wrong about this: 22: static void (*)(void) instruction_table = { jnz, halt, mv, add, mul, mv_reg, add_reg,
10
by: Adam Warner | last post by:
Hi all, Just before Christmas Chris Torek gave me some great advice about closures in C: <http://groups.google.co.nz/groups?selm=cqcl3k030vj%40news3.newsguy.com&output=gplain> It includes this...
2
by: bor_kev | last post by:
Hi, First of all, i want to use the new managed class syntax and STL.NET under Microsoft Visual (C++) Studio 2005 Beta. I read in a Microsoft...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
9
by: johan.tibell | last post by:
I'm in the process of writing an interpreter for lambda calculus (i.e. a small functional programming language) in C. I've previously written one in Haskell so I understand at least some of the...
12
by: Yusuf | last post by:
I'm sorry for the second post in as many hours, and both about structs. The code below compiles. The variables test1 and test2 are structures of datatype teststruct1 and teststruct2. The size of...
7
by: krishna | last post by:
What is the need of this syntax for initializing values, isn't this ambiguous to function call? e.g., int func_name(20); this looks like function call (of course not totally as there is no...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.