Thread: Overloading [] and Assignment

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    Overloading [] and Assignment

    Im working on a small class that mimics an Auto pointer (much like the STD version) but it manages arrays. So I have the operator[] overloaded so I could take out values from the array, I was wondering if there was a way to SET values into an array by overloading something similar to [].

    AutoPtr<int> foo();

    foo[10] = 100;


    is there such thing as operator[]=?
    Founder and avid member of the Internationsl Typo Associateion

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you overloaded the operator [] and if it is returns a refrence to the stored value - you should be able to modify this value as well as retreive it using this operator...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, there's the maxim that subscript operators often come in pairs that might be of interest to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also of interest might by Boost's scoped_array and shared_array, which are smart pointers to arrays. They have different copy semantics than auto_ptr, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Just out of curiosity, is there a way to catch the initialization of an array? for example:

    ArrayPtr<int> a[10];

    From my understanding is that the constructor of ArrayPtr is called 10 times and the array exists outside the bounds of the ArrayPtr object. Is tehre a way to catch this without using static variables or overloading new[]?
    Founder and avid member of the Internationsl Typo Associateion

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Mastadex
    the array exists outside the bounds of the ArrayPtr object.
    The array consists of 10 ArrayPtr objects, in this case. Which may or may not be what you intended, but speaking of "the" ArrayPtr object makes no sense.

    Is tehre a way to catch this without using static variables or overloading new[]?
    Catch what?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You'll probably have to force users of your ArrayPtr class to use this syntax:
    Code:
    ArrayPtr<int> a(10);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Assignment operator overloading
    By hsgw in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2007, 06:44 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Overloading assignment operators
    By bennyandthejets in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2003, 09:29 AM
  5. overloading the assignment operator
    By Unregistered36 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2001, 06:51 AM