A short yet complete set of C++ pointer examples?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I am interested to know if there is a web site, or howto that presents all the different forms of pointer and reference usage in the C++ language. I would also like to know if there is a short accurate discussion of Class instantiation mechanics. Not one focusing on the language rules, but one focusing on the runtime structure and its relationship the the source code. I've found understanding such concepts helps me understand a language better. Any suggestions? STH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFARyJIwX61+IL0QsMRAjsIAJ4ovYeo+iVnW2Oa0KT3eMuVLvS1dACg2bTj qDviEqePS2bkWAWTrkij9co= =9hv2 -----END PGP SIGNATURE-----
Hi, On 04 March 2004 pm 20:34, Steven T. Hatton wrote:
I am interested to know if there is a web site, or howto that presents all the different forms of pointer and reference usage in the C++ language.
I would suggest you to start from pointer in C. Any decent C books should do, but I recommend "The C Programming Language" by Ritchie and Kernighan. Regards, Verdi
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 04 March 2004 07:59 am, Cincai Patron wrote:
Hi,
I would suggest you to start from pointer in C. Any decent C books should do, but I recommend "The C Programming Language" by Ritchie and Kernighan.
I have it right here. I just want something that provides a concise yet exhaustive set of examples. I believe there is actually a fairly limited number of fundamental ways to create and use pointers. I'm looking for the essential minimum.
Regards, Verdi
STH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFARy42wX61+IL0QsMRAmUzAJ9s58+M3586PpAGztRQYKUAPoG7fgCfYSxf SE/cJtsdfNsPZkfrtw3dS2g= =Hv2G -----END PGP SIGNATURE-----
On Thursday 04 March 2004 14:25, Steven T. Hatton wrote:
I have it right here. I just want something that provides a concise yet exhaustive set of examples. I believe there is actually a fairly limited number of fundamental ways to create and use pointers. I'm looking for the essential minimum.
I don't think anbody could truly define such an "essential minimum" - it always depends on what kind of program you intend to write. CU -- Stefan Hundhammer <sh@suse.de> Penguin by conviction. YaST2 Development SuSE Linux AG Nuernberg, Germany
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 04 March 2004 09:06 am, Stefan Hundhammer wrote:
I don't think anbody could truly define such an "essential minimum" - it always depends on what kind of program you intend to write.
I'm speaking in the mathematical sense. I want the set of orthogonal 'axioms'. I'm not looking for pedagogical examples, I'm looking for definitive examples.
Stefan Hundhammer <sh@suse.de> Penguin by conviction.
STH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFARzp0wX61+IL0QsMRAoBNAKCoW1HHBT9nn8fWb0cMKizhZUYLCgCgn2pE CgZb6jDCQn8uuXwWS0MYltQ= =yKuu -----END PGP SIGNATURE-----
Steven T. Hatton wrote:
I would suggest you to start from pointer in C. Any decent C books should do, but I recommend "The C Programming Language" by Ritchie and Kernighan.
I have it right here. I just want something that provides a concise yet exhaustive set of examples. I believe there is actually a fairly limited number of fundamental ways to create and use pointers. I'm looking for the essential minimum.
The short answer is that a pointer can point to any type including a pointer types, classes, class members, functions and class functions. So of course int************************************************************** c; is a pointer. The answer you want is probably the following. Someone can correct me if I get it wrong. First note that the following two are identical in meaning: int const const int Then, for a type or class TYPE (which can itself be a pointer): TYPE* p // a pointer to a TYPE const TYPE* p // a pointer to a constant TYPE TYPE const* p // a pointer to a constant TYPE (hence the bit above) TYPE* const p // a constant pointer to a TYPE you can change *p but not p itself const TYPE* const p// a constant pointer to a constant TYPE TYPE const* const p// a constant pointer to a constant TYPE You can also have pointers to functions: double (*function)(double) // pointer to a function that has a double // argument and returns a double. class members: TYPE A::*d; member functions: double (A::*function)( double ) You can even have a constant pointer to a constant member function returning a constant. I can't remember offhand how to do it. I think it requires a typedef. References are easier because they are always constant. Once defined you cannot make a C++ reference refer to anything different. I think references are restricted to types, classes and pointers, but a reference to a pointer is always unnecessary: TYPE& p // a reference to a TYPE const TYPE& p // a reference to a constant TYPE TYPE const& p // a reference to a constant TYPE I believe you cannot define references to functions, members or member functions. One consequence is that C++ lets you use a pointer to a function without dereferencing it. So, using the definition above, we can write: std::cout << function( 3.1415926536 ) << std::endl; or std::cout << (*function)( 3.1415926536 ) << std::endl; Pointers to members and pointers to member functions are a bit stranger but in the right context are very useful. If B is a subclass of A, then we can assign a pointer to an object of class B to an object of class A but not the other way round. OTOH, we can assign a pointer to a member of A to a pointer to a member of B but not the other way round. -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
On 05 March 2004 am 01:46, John Lamb wrote:
You can also have pointers to functions:
double (*function)(double) // pointer to a function that has a double // argument and returns a double.
class members:
TYPE A::*d;
member functions:
double (A::*function)( double )
Just want to add that in C++, pointer to a non-static member function has the "this" hidden parameter. Regards, Verdi
Cincai Patron wrote:
You can also have pointers to functions:
double (*function)(double) // pointer to a function that has a double // argument and returns a double.
class members:
TYPE A::*d;
member functions:
double (A::*function)( double )
Just want to add that in C++, pointer to a non-static member function has the "this" hidden parameter.
I don't think I understood that one. :-/ -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
On 05 March 2004 pm 17:00, John Lamb wrote:
Just want to add that in C++, pointer to a non-static member function has the "this" hidden parameter.
I don't think I understood that one. :-/
What I mean is, to have a pointer to a member function, we must include the object as the first parameter. Regards, Verdi
Cincai Patron wrote:
On 05 March 2004 pm 17:00, John Lamb wrote:
Just want to add that in C++, pointer to a non-static member function has the "this" hidden parameter.
I don't think I understood that one. :-/
What I mean is, to have a pointer to a member function, we must include the object as the first parameter.
Good point. It's worthwhile emphasising that you cannot use a pointer to a member without an object (though you can declare one). I learned this one the hard way. I confused myself thinking about the equivalence of x and this->x. :-) -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
Cincai Patron wrote:
On 05 March 2004 pm 17:00, John Lamb wrote:
Just want to add that in C++, pointer to a non-static member function has the "this" hidden parameter.
I don't think I understood that one. :-/
What I mean is, to have a pointer to a member function, we must include the object as the first parameter.
Good point. It's worthwhile emphasising that you cannot use a pointer to a member without an object (though you can declare one). I learned this one the hard way.
I confused myself thinking about the equivalence of x and this->x. :-)
-- JDL I'm not sure if it's related, but it seems close. Stroustrup uses ::operator+(X(1,1.0)); in a comment to suggest the use of the globally defined addition operator. See section 11.2.1 of TC++PL, SE. In JavaScript
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 05 March 2004 12:58 pm, John Lamb wrote: there is always a mother of all objects object. I wonder if the same is true of C++. IOW, are all global variables actually members of an object? I'm not much closer to my original goal of determining a set of pointer axioms. I believe it is correct to consider an array identifier to be a constant pointer to the type of the array, and pointing to the first element of that array. I started to create a parser that would diagram the precedence of pointer expressions, but there are so many things I don't understand about C++, I decided to step back from that effort. STH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFATQVcwX61+IL0QsMRAq0WAKDZo+SqUX9KT/0iFC70RkoaPwOqSgCgkZs+ tWb+vryqW2HoyTPIoqXXQ0w= =EOLf -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm just going to add this to the collection: - From 11.10 of TC++PL, SE p->m == (*p).m == p[0].m In general the notion of overloading '->' frightens me, and I believe I will avoid it for the forseeable future. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFAUV5VwX61+IL0QsMRAgHbAKC/629Lf0G0dBu054dkGcgbg7Hg0wCgnaJw BrGjig7wj1bQVWo+5jlg4iw= =0p+E -----END PGP SIGNATURE-----
On Thu, 4 Mar 2004 20:59:47 +0800 Cincai Patron <cincaipatron@gmx.net> wrote:
I would suggest you to start from pointer in C. Any decent C books should do, but I recommend "The C Programming Language" by Ritchie and Kernighan. K&R is C, Steven is asking some very C++ specific stuff. While both C and C++ share pointer syntax, references are specific to C++.
Here are some websites I use, but I'm not thrilled with them. http://www.cpp-home.com/ http://www.parashift.com/c++-faq-lite/ http://www.yrl.co.uk/~phil/stl/stl.htmlx http://www.accu.org/ http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/doc/doc.html http://www.cs.uiowa.edu/~rlawrenc/teaching/030/Notes/ -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 05 March 2004 08:34 pm, Jerry Feldman wrote:
On Thu, 4 Mar 2004 20:59:47 +0800
Cincai Patron <cincaipatron@gmx.net> wrote:
I would suggest you to start from pointer in C. Any decent C books should do, but I recommend "The C Programming Language" by Ritchie and Kernighan.
K&R is C, Steven is asking some very C++ specific stuff. While both C and C++ share pointer syntax, references are specific to C++.
Here are some websites I use, but I'm not thrilled with them. http://www.cpp-home.com/ http://www.parashift.com/c++-faq-lite/ http://www.yrl.co.uk/~phil/stl/stl.htmlx http://www.accu.org/ http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/doc/doc.html http://www.cs.uiowa.edu/~rlawrenc/teaching/030/Notes/
We might want to consider putting together a consolidated collection of good on-line resources. I've come across some good ones. I haven't read much of this, but the parts I have read were very enlightening. I am playing with the second part of Chapter 8 right now. http://www-aix.gsi.de/~razumov/C++annotations/cplusplus.html#toc There are some dead links here, but also some gems: http://www.fz-juelich.de/zam/cxx/extern.html STH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFAS13ywX61+IL0QsMRAm5BAKDYvz+76j5XV8tNysyJYPxvs23wFACg8dvL ai8bVua3r6Y7IIo/tCl6vxY= =KG6t -----END PGP SIGNATURE-----
participants (5)
-
Cincai Patron
-
Jerry Feldman
-
John Lamb
-
Stefan Hundhammer
-
Steven T. Hatton