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



 DEVELOP > c-Plus-Plus > void *pfoo=X::foo; // error,help me pls

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 28 Jan 2008 05:52:42 AM
Object: void *pfoo=X::foo; // error,help me pls
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 :

.

User: "Alf P. Steinbach"

Title: Re: void *pfoo=X::foo; // error,help me pls 28 Jan 2008 06:02:55 AM
* lishujun2006@gmail.com:

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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: ""

Title: Re: void *pfoo=X::foo; // error,help me pls 28 Jan 2008 07:06:47 AM
Damn thats some complicated thing oO.
.


User: "tOmMy"

Title: Re: void *pfoo=X::foo; // error,help me pls 28 Jan 2008 09:31:02 AM
I don't know why do you want a void pointer to point to a specified
class member function.
On 1=D4=C228=C8=D5, =CF=C2=CE=E77=CA=B152=B7=D6, "
" <l=
ishujun2...@gmail.com>
wrote:

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

};

void main(){
X x;
void *pfoo =3D 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 :


.
User: "=?utf-8?Q?David_C=C3=B4me?="

Title: Re: void *pfoo=X::foo; // error,help me pls 28 Jan 2008 11:19:49 AM
On Mon, 28 Jan 2008 16:31:02 +0100, tOmMy <eraccn@gmail.com> wrote:

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


On 1月28日, 下午7时52分, "

"
<
>
wrote:

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 :



struct X
{
void foo(void){...}
};
int main (void)
{
void (X::* ptr)(void) ;
ptr=&X::foo;
X x;
(x.*ptr)();
return 0;
}
.
User: "Jim Langston"

Title: Re: void *pfoo=X::foo; // error,help me pls 28 Jan 2008 12:14:03 PM
David Cme wrote:

On Mon, 28 Jan 2008 16:31:02 +0100, tOmMy <eraccn@gmail.com> wrote:

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


On 1?28?, ??7?52?, "

"
<
>
wrote:

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 :





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.
--
Jim Langston
tazmaster@rocketmail.com
.
User: "James Kanze"

Title: Re: void *pfoo=X::foo; // error,help me pls 28 Jan 2008 04:45:06 PM
On Jan 28, 7:14 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:

David C=F4me wrote:

On Mon, 28 Jan 2008 16:31:02 +0100, tOmMy <era...@gmail.com> wrote:

I don't know why do you want a void pointer to point to a specified
class member function.
On 1?28?, ??7?52?, "

"
<
>
wrote:

class X{
void foo(){...}
};
void main(){
X x;
void *pfoo =3D 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 :


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

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

};
int main (void)

Same thing here.

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

And here.

ptr=3D&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.

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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "=?utf-8?Q?David_C=C3=B4me?="

Title: Re: void *pfoo=X::foo; // error,help me pls 29 Jan 2008 01:36:49 PM
On Mon, 28 Jan 2008 23:45:06 +0100, James Kanze <james.kanze@gmail.com>
wrote:

On Jan 28, 7:14 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:

David Côme wrote:

On Mon, 28 Jan 2008 16:31:02 +0100, tOmMy <era...@gmail.com> wrote:


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


On 1?28?, ??7?52?, "

"
<
>
wrote:

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 :



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


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

};


int main (void)


Same thing here.

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


And here.

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.


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.

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.
.
User: "James Kanze"

Title: Re: void *pfoo=X::foo; // error,help me pls 30 Jan 2008 04:23:44 AM
On Jan 29, 8:36 pm, David C=F4me <davidc...@wanadoo.fr> wrote:

On Mon, 28 Jan 2008 23:45:06 +0100, James Kanze
<james.ka...@gmail.com> wrote:

On Jan 28, 7:14 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:

David C=F4me wrote:

[...]

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

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

};


int main (void)

Same thing here.

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

And here.

ptr=3D&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.

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.

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.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "=?utf-8?Q?David_C=C3=B4me?="

Title: Re: void *pfoo=X::foo; // error,help me pls 30 Jan 2008 05:48:19 AM
On Wed, 30 Jan 2008 11:23:44 +0100, James Kanze <james.kanze@gmail.com>
wrote:
<SNIP>


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.
.




User: "=?utf-8?Q?David_C=C3=B4me?="

Title: Re: void *pfoo=X::foo; // error,help me pls 28 Jan 2008 01:15:35 PM
On Mon, 28 Jan 2008 19:14:03 +0100, Jim Langston
<tazmaster@rocketmail.com> wrote:

David Côme wrote:

On Mon, 28 Jan 2008 16:31:02 +0100, tOmMy <eraccn@gmail.com> wrote:

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


On 1?28?, ??7?52?, "

"
<
>
wrote:

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 :





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.

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





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER