linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Request help with function pointers
@ 2007-02-22  4:27 Shriramana Sharma
  2007-02-27 14:48 ` Stephen Kratzer
  2007-02-27 15:15 ` Stephen Kratzer
  0 siblings, 2 replies; 5+ messages in thread
From: Shriramana Sharma @ 2007-02-22  4:27 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 948 bytes --]

Hello. I am trying the examples from Bruce Eckel's Thinking in C++ 2nd 
edn and am getting a problem. I have attached the examples I am using 
and the error messages (basically the same kind) which I got. I am 
unable to understand what is wrong when I have followed the book example 
perfectly (AFAIK) except for the variablenames which is immaterial.

Please explain what I did wrong. I am using:

$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v 
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr 
--enable-shared --with-system-zlib --libexecdir=/usr/lib 
--without-included-gettext --enable-threads=posix --enable-nls 
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu 
--enable-libstdcxx-debug --enable-mpfr --enable-checking=release 
i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)

Thank you.

Shriramana Sharma.

[-- Attachment #2: function-pointers-1.cpp --]
[-- Type: text/x-c++src, Size: 936 bytes --]

#include "stdio.h"

class myclass
{
public :
	void f ( int ) ;
	void g ( int ) ;
	void h ( int ) ;
	void i ( int ) ;
} ;

void myclass :: h ( int a ) { printf ( "%d", a ) ; }

int main ( void )
{
	myclass myclassinstance ;
	myclass * myclassinstancepointer = & myclassinstance ;
	void ( myclass :: * pointertofunction ) ( int ) = & myclass :: h ;
	// this means that pointertofunction is a pointer (*) to a function existing in the scope of myclass (myclass ::)
	// and takes an int as input ( (int) ) and gives void as output ( void )
	( myclassinstance . * pointertofunction ) ( 1 ) ; 
	// calls the function pointed to by pointertofunction and existing as a member of the class myclassinstance with 1 as the parameter
	( myclassinstancepointer -> * pointertofunction ) ( 2 ) ;
	// calls the function pointed to by pointertofunction and existing as a member of the class pointed to by myclassinstancepointer with 2 as the parameter
}

[-- Attachment #3: function-pointers-1-error --]
[-- Type: text/plain, Size: 464 bytes --]

samjnaa@chandas:~/bn/learning$ g++ function-pointers-not-working.cpp -o fp
function-pointers-not-working.cpp: In function ���������int main()���������:
function-pointers-not-working.cpp:21: error: expected unqualified-id before ���������*��������� token
function-pointers-not-working.cpp:23: error: expected unqualified-id before ���������*��������� token

[-- Attachment #4: function-pointers-2.cpp --]
[-- Type: text/x-c++src, Size: 1500 bytes --]

#include "stdio.h"

class maths
{
private :
	void square ( int a ) const { printf ( "The square of %d is %d\n", a, a * a ) ; }
	void cube ( int a ) const { printf ( "The cube of %d is %d\n", a, a * a * a ) ; }
	void fourthpower ( int a ) const { printf ( "The fourth power of %d is %d\n", a, a * a * a * a ) ; }
	void ( maths :: * functionpointers [ 3 ] ) ( int ) const ;
	/*
	the above line declares an array called functionpointers that has three ( 3 ) elements which are all pointers ( * ) to functions in the scope of the class maths ( maths :: ) and take a single integer as parameter ( int ) and return nothing ( void ) and do not modify the contents of the class in anyway ( const )
	*/
public :
	maths ()
	{
		functionpointers [ 0 ] = & maths :: square ;
		functionpointers [ 1 ] = & maths :: cube ;
		functionpointers [ 2 ] = & maths :: fourthpower ;
	}
	void executefunction ( int functionnumber, int inputforfunction )
	{
		if ( functionnumber < 0 || functionnumber > 2 ) return ;
		( this -> * functionpointers [ functionnumber ] ) ( inputforfunction ) ;
		// this line calls the function which is pointed ( * ) to by the functionnumber-th element of the functionpointers array which is a member of the object pointed to by the current object pointer ( this ) and passes the function the value of the variable inputforfunction as a parameter
	}
} ;

int main ( void )
{
	maths mathsinstance ;
	for ( int count = 0; count < 3; count ++ )
		mathsinstance . executefunction ( count, 2 ) ;
}

[-- Attachment #5: function-pointers-2-error --]
[-- Type: text/plain, Size: 337 bytes --]

samjnaa@chandas:~/bn/learning$ g++ function-pointers-2.cpp -o fp2 -Wall
function-pointers-2.cpp: In member function ���������void maths::executefunction(int, int)���������:
function-pointers-2.cpp:23: error: expected unqualified-id before ���������*��������� token

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Request help with function pointers
  2007-02-22  4:27 Request help with function pointers Shriramana Sharma
@ 2007-02-27 14:48 ` Stephen Kratzer
  2007-02-28 15:32   ` Shriramana Sharma
  2007-02-27 15:15 ` Stephen Kratzer
  1 sibling, 1 reply; 5+ messages in thread
From: Stephen Kratzer @ 2007-02-27 14:48 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming

On Wednesday 21 February 2007 23:27, Shriramana Sharma wrote:
> Hello. I am trying the examples from Bruce Eckel's Thinking in C++ 2nd
> edn and am getting a problem. I have attached the examples I am using
> and the error messages (basically the same kind) which I got. I am
> unable to understand what is wrong when I have followed the book example
> perfectly (AFAIK) except for the variablenames which is immaterial.
>
> Please explain what I did wrong. I am using:
>
> $ g++ -v
> Using built-in specs.
> Target: i486-linux-gnu
> Configured with: ../src/configure -v
> --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
> --enable-shared --with-system-zlib --libexecdir=/usr/lib
> --without-included-gettext --enable-threads=posix --enable-nls
> --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
> --enable-libstdcxx-debug --enable-mpfr --enable-checking=release
> i486-linux-gnu
> Thread model: posix
> gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
>
> Thank you.
>
> Shriramana Sharma.

Remove the space in ". *" and "-> *".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Request help with function pointers
  2007-02-22  4:27 Request help with function pointers Shriramana Sharma
  2007-02-27 14:48 ` Stephen Kratzer
@ 2007-02-27 15:15 ` Stephen Kratzer
  1 sibling, 0 replies; 5+ messages in thread
From: Stephen Kratzer @ 2007-02-27 15:15 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming

On Wednesday 21 February 2007 23:27, Shriramana Sharma wrote:
> Hello. I am trying the examples from Bruce Eckel's Thinking in C++ 2nd
> edn and am getting a problem. I have attached the examples I am using
> and the error messages (basically the same kind) which I got. I am
> unable to understand what is wrong when I have followed the book example
> perfectly (AFAIK) except for the variablenames which is immaterial.
>
> Please explain what I did wrong. I am using:
>
> $ g++ -v
> Using built-in specs.
> Target: i486-linux-gnu
> Configured with: ../src/configure -v
> --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
> --enable-shared --with-system-zlib --libexecdir=/usr/lib
> --without-included-gettext --enable-threads=posix --enable-nls
> --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
> --enable-libstdcxx-debug --enable-mpfr --enable-checking=release
> i486-linux-gnu
> Thread model: posix
> gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
>
> Thank you.
>
> Shriramana Sharma.

Remove the space in ". *" and "-> *".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Request help with function pointers
  2007-02-27 14:48 ` Stephen Kratzer
@ 2007-02-28 15:32   ` Shriramana Sharma
  2007-03-07 16:49     ` Glynn Clements
  0 siblings, 1 reply; 5+ messages in thread
From: Shriramana Sharma @ 2007-02-28 15:32 UTC (permalink / raw)
  To: linux-c-programming

Stephen Kratzer wrote:
> Remove the space in ". *" and "-> *".

Thank you very much -- that worked, but is there any good reason why I
should not be allowed to put a space there? When I can use:

:: *

in the same programs without an error, why can't I use

. *

or

-> *

(I like having my code neatly and generously spaced since I feel it
looks cleaner.)

I get no problem if I space the . and -> in other cases like:

QWidget . show () ;

or

this -> show () ;

So if there is no good reason for not being allowed to put a space
between . and * or -> and *, I can report a bug against gcc asking for
this error to be removed for being meaningless.

Thank you.

Shriramana Sharma.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Request help with function pointers
  2007-02-28 15:32   ` Shriramana Sharma
@ 2007-03-07 16:49     ` Glynn Clements
  0 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2007-03-07 16:49 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: linux-c-programming


Shriramana Sharma wrote:

> > Remove the space in ". *" and "-> *".
> 
> Thank you very much -- that worked, but is there any good reason why I
> should not be allowed to put a space there? When I can use:
> 
> :: *
> 
> in the same programs without an error, why can't I use
> 
> . *
> 
> or
> 
> -> *

Because ".*" and "->*" are tokens, and you can't have a space in the
middle of an token, in the same way that you can't put a space in the
middle of "++", "+=", ">=" etc.

".*" looks like a combination of "." and "*", and "->*" looks like a
combination of "->" and "*", and in a sense they behave that way, but
".*" and "->*" are actually separate tokens, not a combination.

> (I like having my code neatly and generously spaced since I feel it
> looks cleaner.)
> 
> I get no problem if I space the . and -> in other cases like:
> 
> QWidget . show () ;
> 
> or
> 
> this -> show () ;

Here, "." and "->" are tokens. Putting a space between the "-" and ">"
in "->" would similarly result in an error, as would a space in the
middle of "th is". You can put a space between "(" and ")", though.

> So if there is no good reason for not being allowed to put a space
> between . and * or -> and *, I can report a bug against gcc asking for
> this error to be removed for being meaningless.

No, gcc is correct.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-03-07 16:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-02-22  4:27 Request help with function pointers Shriramana Sharma
2007-02-27 14:48 ` Stephen Kratzer
2007-02-28 15:32   ` Shriramana Sharma
2007-03-07 16:49     ` Glynn Clements
2007-02-27 15:15 ` Stephen Kratzer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).