My own variable - the array

This is a discussion on My own variable - the array within the C++ Programming forums, part of the General Programming Boards category; Ok, so I was creating a program and I ran into a problem. I needed to return an array. Now, ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    892

    My own variable - the array

    Ok, so I was creating a program and I ran into a problem. I needed to return an array. Now, usually, I'd just return a pointer, but under the circumstances, I couldn't. So I descided to use the beauty of OOP and create my own variable that carries array info.

    So here's my question. How do I program the '[' and ']' operators. Or, even more so, how do I create a variable? This is all I have so far:

    PHP Code:
    class array
    {
        
    int length;
        
    int value[100];

        public:
        array(
    int a[])
        {
            for(
    int x 0100x++)
            {
                if(!
    a[x] == NULL)
                {
                    
    a[x] = value[x];
                }
                else
                {
                    break;
                }
            }
        }

    }; 
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Why don't you just make the array public and then you can do whatever you want with the . operator. By the way, in this case, u dont need to overload the [] operator because you are only creating arrays of ints.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    892
    I cannot make it global. It would not fit my idea for the program. Plus, you do need '[]'. Try this:

    //with the class.....

    PHP Code:
    int main()
    {
    array 
    bob = new int[34];
    bob[23] = 7;
    return(
    0);

    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Like this -

    Code:
    #include <iostream>
    
    using namespace std;
    
    class array
    {
        int length;
        int value[100];
    
        public:
        array(int a[])
        {
            for(int x = 0; x < 100; x++)
            {
                value[x]=a[x];
               
            }
        }
    
    	int& operator[](int ind)
    	{
    		return value[ind];
    	}
    
    };
    
    int main(){
    
    	int arr[100]={0};
    	array a(arr);
    
    	a[0]=10;
    	cout << a[0];
    
    	
    	
      return 0;
    }
    edit - your constructor was broken, and you can't really test for NULL in an array of ints. Also there's a perfectly good array container in the standard library - std::vector.
    Last edited by Sorensen; 03-03-2002 at 03:44 PM.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    892
    Thank you. That's just what I needed.

    Say, does anybody know of any good tutorials on declaring operators, etc.?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I was thinking of something else, sorry. BTW, goto this site: http://www.syndik.at/harald/regal/buecher/login_e.asp. Inside, there is a book called Using C++. Chapter 14 has an entire chapter on it.

  7. #7
    Señor Member
    Join Date
    Jan 2002
    Posts
    561
    If you want a good example of what you are making right now try looking at apvector.h and apvector.cpp. You should be able to find them on google or if you can't find it there, it's somewhere on www.collegeboard.com.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Joining array elements and save in variable.
    By Nutshell in forum C Programming
    Replies: 12
    Last Post: 01-12-2002, 12:06 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM

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