I agree with SixDegrees, invalid references could be even more painful to debug, consider this:
#include <iostream>
class Test{
public:
int x;
};
int main( int argc, char ** argv ){
Test * test = new Test();
Test& ref = *test;
ref.x = 10;
std::cout << "value is " << ref.x;
delete test;
std::cout << "\nwhat now ? " << ref.x;
// test->x = 0; // this will cause crash on my machine, line above not
return 0;
}
#include <iostream>
class Test{
public:
int x;
};
int main( int argc, char ** argv ){
Test * test = new Test();
Test& ref = *test;
ref.x = 10;
std::cout << "value is " << ref.x;
delete test;
std::cout << "\nwhat now ? " << ref.x;
// test->x = 0; // this will cause crash on my machine, line above not
return 0;
}
To copy to clipboard, switch view to plain text mode
on my machine it prints:
value is 10
what now ? 7409624
program runs without complains, while the "pointer" version nicely crashes. This is very simple example, but try to debug such errors with more complicated code. Not very pleasant experience.
Bookmarks