void *pfoo=X::foo; // error,help me pls

L

lishujun2006

class X{
void foo(){...}
};

void main(){
X x;
void *pfoo = X::foo;
//but ,error in VC and C++ Builder
((void(*)(X*))pfoo)(&x);
//call member function
}

sorry , i am english is very poor,but ,i need help,thanks,everyone

my email is : (e-mail address removed)
 
A

Alf P. Steinbach

* (e-mail address removed):
class X{
void foo(){...}
};

void main(){
X x;
void *pfoo = X::foo;
//but ,error in VC and C++ Builder
((void(*)(X*))pfoo)(&x);
//call member function
}

sorry , i am english is very poor,but ,i need help,thanks,everyone

As a novice, don't use pointers to member functions (or raw pointers in
general).

What is the problem you're trying to solve?

As opposed to above attempted technical solution.


Cheers, & hth.,

- Alf
 
T

tOmMy

I don't know why do you want a void pointer to point to a specified
class member function.
 
D

David Côme

I don't know why do you want a void pointer to point to a specified
class member function.


struct X
{
void foo(void){...}
};

int main (void)
{
void (X::* ptr)(void) ;

ptr=&X::foo;
X x;

(x.*ptr)();

return 0;
}
 
J

Jim Langston

David said:
struct X
{
void foo(void){...}
};

int main (void)
{
void (X::* ptr)(void) ;

ptr=&X::foo;
X x;

(x.*ptr)();

return 0;
}

There is a problem with that code. The signature of the function foo is not
actually void foo(void) but void foo(X*) because a non static class member
has a hidden this pointer. And the compiler actually looks at the function
pointer as void X::foo( void ) I believe.
 
D

David Côme

There is a problem with that code. The signature of the function foo is
not
actually void foo(void) but void foo(X*) because a non static class
member
has a hidden this pointer. And the compiler actually looks at the
function
pointer as void X::foo( void ) I believe.

This code compile fine with g++ 4.2.3 and is full standard compatible.
 
J

James Kanze

I'd drop the second void here... It makes you look like a C
programmer who doesn't know C++.

Same thing here.

And here.
There is a problem with that code. The signature of the
function foo is not actually void foo(void) but void foo(X*)
because a non static class member has a hidden this pointer.

The signature of the function is void X::foo(), not void
foo(X*). They're two different things. The correct way to
declare a pointer to a member function is "void (X::*ptr)()",
basically as he's done.
And the compiler actually looks at the function pointer as
void X::foo( void ) I believe.

Exactly. And not as void foo(X*), which would be something else
again.
 
D

David Côme

I'd drop the second void here... It makes you look like a C
programmer who doesn't know C++.


Same thing here.


And here.



The signature of the function is void X::foo(), not void
foo(X*). They're two different things. The correct way to
declare a pointer to a member function is "void (X::*ptr)()",
basically as he's done.


Exactly. And not as void foo(X*), which would be something else
again.

I've written this code in 2 mins.Excuse me if it contains some
imprecisions.
So :

-I've used struct because I didn't want to type
class X
{
public:
//...
};

That's all.For me, the only difference between struct and class is the
default visibility of members.
Is it true ?

-main-.
In my small prog, I don't use argc and argv.So I prefer use (void) instead
of (int argc,char *argv[])

-(void) instead of ()
I had written a lot of C code before I started to use C++. So I'm used to
write (void) instead of ().
I know it's a mistake but not a huge one.
 
J

James Kanze

[...]
I've written this code in 2 mins.Excuse me if it contains some
imprecisions.
So :
-I've used struct because I didn't want to type
class X
{
public:
//...
};
That's all.For me, the only difference between struct and class is the
default visibility of members.
Is it true ?

Yes, and using struct in cases where we are discussing language
issues where access is not the question (and we are not
developing real code) is a widespread convention.
-main-.
In my small prog, I don't use argc and argv.So I prefer use (void) instead
of (int argc,char *argv[])
-(void) instead of ()
I had written a lot of C code before I started to use C++. So I'm used to
write (void) instead of ().
I know it's a mistake but not a huge one.

Not a "mistake" as such, but it does tend to mark you as a "C
hacker", rather than a C++ programmer. (I'll admit that I never
got into the habit because I started using C++ before I had
access to ANSI C compilers.) The original C++ compilers didn't
allow void here---the language was only extended to support it
for reasons of C compatibility. (For that matter, a lot of
people in the C community didn't like the void here either. It
was just that there was no other way of doing it without
breaking every C program in existance at the time. A necessary
evil, and not something to be proud of.)
 
D

David Côme

said:
Not a "mistake" as such, but it does tend to mark you as a "C
hacker", rather than a C++ programmer. (I'll admit that I never
got into the habit because I started using C++ before I had
access to ANSI C compilers.) The original C++ compilers didn't
allow void here---the language was only extended to support it
for reasons of C compatibility. (For that matter, a lot of
people in the C community didn't like the void here either. It
was just that there was no other way of doing it without
breaking every C program in existance at the time. A necessary
evil, and not something to be proud of.)
Ok. I didn't know that.
 

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
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top