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
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