Log in

View Full Version : Why are C++ pointers not overloaded?



Trinithis
10-12-2007, 04:09 AM
Background info: I'm fairly new to C++ pointers. Despite this, I have a solid background in references regarding Javascript and Java.

Is there a reason why one needs to explicitly get the address of a variable with the & operator to store it into a pointer? Is it simply that C++ wasn't designed to be smart enough to do so and now it is too late to add the feature? Or is there a genuine reason/use for such assignment to be syntaxed that way? The same questions apply to dereferencing as well.

Example real code:


int x = 5;
int* px = &x;
int y = *px;
int* py = px;

Foo* f = new Foo();
(*f).method();


vs. the hypothetical code:


int x = 5;
int* px = x; //overloading '=' operator
int y = px; //more overloading '=' operator
int* py = px;

Foo* f = new Foo();
f.method(); //overloading '.' operator

Twey
10-12-2007, 05:01 PM
Is there a reason why one needs to explicitly get the address of a variable with the & operator to store it into a pointer?Because C++ is a fairly low-level language, and as such there are cases where one would want to work with that address itself rather than the value stored therein. Automatically dereferencing would introduce ambiguity and possibly disallow pointer arithmetic.