473,387 Members | 1,534 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,387 software developers and data experts.

pointer to member / strange construction

Can someone explain the following construct to me:
What is not clear to me is what the argument to foo is.
A pointer to a member?

template <class U>
static char foo(void (U::*)(void));

regards,
alex
Jul 19 '05 #1
2 2973
What looks strange to me is the U::* part. No function name, only
a class qualifier? Could you please give more detail or tell me, where to
look up in the standard.
template <class U>
static char foo(void (U::*)(void));


Yes, it's a pointer to a member function of U. The function
takes no arguments and returns nothing. You could use it
this way:

struct realU {
void bar();
};
...
char c = foo(&realU::bar);

(the compiler should be able to derive 'U == realU' here)

Victor


Alex
Jul 19 '05 #2
"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote...
What looks strange to me is the U::* part. No function name, only
a class qualifier? Could you please give more detail or tell me, where to
look up in the standard.
Please don't top-post.

The declaration of a formal argument does not require a name.
If I were to declare a stand-alone variable, I'd give it a name:

void (MyClass::*ptrToMemOfMyClass)(); // name is required

if the same declaration (of a pointer to a member) would happen
to be part of a function declaration, you could drop the name:

void foo( void (MyClass::*)() ); // foo is a function
^^^^^^^^^^^^^^^^^^^

As to the asterisk, are you familiar with the syntax

"<type-id> (*)(<arg-list>)"?

Example:

void foo(int (*)(double, char));
^^^^^^^^^^^^^^^^^^^^^

? In the declaration above the argument 'foo' takes is
a pointer to a function that takes two arguments and returns
int. Now, the only difference with a member function is that
it is prefixed with the class name and the scope resolution
operator:

void foo(int (MyClass::*)(double, char));

which means that the pointer is NOT to a regular function, but
to a non-static member function of MyClass.

The formal argument name is optional when declaring a function,
so the next two declarations are equivalent:

int foo(double d, char c);
int foo(double, char);

The same with pointers to functions:

int foo(int (*myfuncptr)(double,char));
int foo(int (*)(double,char));

('myfuncptr' is the name of the formal argument).

Relevant sections of the Standard include 8.3.5, 8.3.1, 8.3.3
(and all they reference).
template <class U>
static char foo(void (U::*)(void));


Yes, it's a pointer to a member function of U. The function
takes no arguments and returns nothing. You could use it
this way:

struct realU {
void bar();
};
...
char c = foo(&realU::bar);

(the compiler should be able to derive 'U == realU' here)

Victor


Alex


Victor
Jul 19 '05 #3

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

Similar topics

4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
7
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really...
6
by: Peter Oliphant | last post by:
Here is a simplification of my code. Basically, I have a class (A) that can be constructed using a function pointer to a function that returns a bool with no parameters. I then want to create an...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
4
by: majsta | last post by:
hello, I have probably a stupid question, but I don't get one thing. In the following source, the first constructor "does not work" - it creates a pointer variable pI, sets ii as *pI, but this...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.