Thread: about operators

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    98

    about operators

    I've created a class (Array) and i added the operator [] to get a specified element from the array.
    i also want to add the operator = to create\change an element i get from the [] operator, like that:
    Code:
    Array<int> arr;
    arr[0] = 7;
    how can i do it?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The assignment operator (operator =) would be so you could do this:
    Code:
    Array<int> bob = {1,3,5,6};
    Array<int> joe;
    joe = bob;
    When you have
    bob[0] = 7
    then that = is taken care of by int

    Just make sure that operator[] returns a reference and you'll be set.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    i don't understand how to do it, coz the assignment operator takes Array type value by default, right? so why should he be applied to a reference?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What you showed in your example was not assigning one Array to another but assigning an int to a member inside of the Array.
    So by having operator[] return a reference to the object it can be used as a l-value.

    If you have a operator= for the Array class then that will take in a const Array reference and basically copy the given array like I showd in joe = bob;

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    but if i want to create new elements that way? i can use this only for modifying existing elements, but not for creating new ones..
    besides, i still didn't understand how to do what u said, could u give me code example for the operators plz?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To add elements you'll need to write an insert method. You can't really create "new" elements using operator[] as the object has to already exist.

    Have a look at the interface for a vector which is basically a resizable array
    http://www.cppreference.com/cppvector/

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    i decided i don't want it to assign new elements, so thanks for ur help =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 07:35 PM