*) Can u please let me know, How to find address of class or any non-abstract data type ?

*) Reference type is an alias to an object, what does it mean ? Where this alias store in memory?

These both questions are C++ domain. Please give us examples.

Varun Jain

1/ Types do not have addresses. Objects (instances of types) have addresses. To find the address of an object use the & unary operator:

       T a_t_instance;
       T * a_t_pointer( NULL );
       a_t_pointer = &a_t_instance;

Using the & unary operator returns a pointer to the type of the object the operator is applied to. The unary * operator does the opposite - it returns the value of what a pointer points to:

       int j(10);
       int * j_ptr( &j );
       std::cout << j_ptr << "\n";  // prints the address of j
       std::cout << *j_ptr << "\n"; // prints 10

2/ A C++ reference type always refers to an existing object, so must always be initialised. Unlike pointer types a reference type has no null value. Also unlike pointer types you do not need to use the & operator to obtain a reference and * to de-reference a reference. In this respect a reference to an object is like another name (or alias) for it:

       T a_t_instance;
       T & a_t_reference( a_t_instance );

The alias itself is probably implemented much like a pointer, if the compiler has to make it explicit, as would be the case if it were passed to a function or the object was passed to the function as a reference. However the point to note is all such aliases refer to the original object - which exists at its location in memory. Like pointers, passing around references to large objects is a lot more efficient than passing copies of the object itself. Modifying a reference modifies the original object -like modifying what a pointer points at.

Unlike pointers a reference cannot be null, must always be initialised on creation and cannot have what it refers to changed once it is created. Also unlike pointers using a reference always means accessing the object to which it refers.

The following shows various uses of references and pointers:

       int i(10);
       int & i_ref(i); // Reference must be initialised
       int * i_ptr;    // Pointer need not be initialised (dangerous)
       i_ptr = &i;     // Can assign pointers (address) values

       std::cout << i_ref << "\n";   // prints 11

       std::cout << i << "\n";       // prints 12

       std::cout << i_ptr << "\n";   // prints address of i
       std::cout << *i_ptr << "\n";  // prints 12

       ++*i_ptr; // yuk! Increments i through i_ptr.
       std::cout << i << "\n";       // prints 13

       int j(123);
       i_ptr = &j; // i_ptr is now a pointer to j

       std::cout << i_ptr << "\n";   // prints address of j

       ++*i_ptr; // Increments j through i_ptr.
       std::cout << *i_ptr << "\n";  // prints 124

Note that references are easier to use if you wish to manipulate the object they refer to, but pointers allow more flexibility in that the can be re-assigned, and in fact can have arithmetic performed on them when using blocks (arrays) of objects.

Hope this helps a little.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.