Overloading [] and Assignment

This is a discussion on Overloading [] and Assignment within the C++ Programming forums, part of the General Programming Boards category; Im working on a small class that mimics an Auto pointer (much like the STD version) but it manages arrays. ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Incidentally, there's the maxim that subscript operators often come in pairs that might be of interest to you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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,893
    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,317
    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, 05: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, 05:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21