Results 1 to 2 of 2

Thread: Why are C++ pointers not overloaded?

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Why are C++ pointers not overloaded?

    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:
    Code:
    int x = 5;
    int* px = &x;
    int y = *px;
    int* py = px;
    
    Foo* f = new Foo();
    (*f).method();
    vs. the hypothetical code:
    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
    Last edited by Trinithis; 10-14-2007 at 08:13 PM.
    Trinithis

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •