Originally Posted by
Elysia
It means that operator [] dereferences the pointer.
p[0] == *p
*p[0] == **p
Compile first, kids.
Code:
#include <iostream>
#include <ostream>
#include <cstddef>
class array
{
public:
explicit array ( std::size_t sz ): p( new int[sz] ), size( sz )
{
if ( size <= 0 ) {
throw length_error();
}
}
~array ( ) { delete [] p; }
const int & operator[] ( std::size_t idx ) const { return p[idx]; }
int & operator[] ( std::size_t idx ) { return p[idx]; }
// ...
private:
class length_error { };
int * p;
size_t size;
};
int main ( )
{
array * stak = new array( 5 );
std::cout << ( **stak )[0] << std::endl;
}
Compiling...
learn.cpp
c:\documents and settings\jck\my documents\visual studio 2005\projects\learn\learn\learn.cpp(27) : error C2100: illegal indirection
Build log was saved at "file://c:\Documents and Settings\JCK\My Documents\Visual Studio 2005\Projects\learn\learn\Debug\BuildLog.htm"
learn - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Price is right fail music would be nice here.