How do I have access to private variables in classes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlarV
    New Member
    • Dec 2008
    • 37

    How do I have access to private variables in classes?

    I want to create an array of pointers, for my class. Each pointer will in fact be the 'head' of a dynamic list. I don't know the size of the array and it has to be given by the user.

    So my problem is that in my class I have to read the size in the constructor. In the other functions of the class( e.g. insert etc.) the table has to EXIST. However, when I try to access it from the other functions (e.g. insert) the table isn't NULL. And the program crashes. Are there any solutions?

    Here is a picture of what I want to create:


    Here is the code:
    Code:
    #ifndef hashdef
    #define hashdef
    
    using namespace std;
    
    class hash
    {
       int i;
       int size;
       class hash *table[ ];
       public:
          hash();
          void insert(int a);
          void deletekey(int key);
          bool search(int a);
          void destroy();
    };
    
    hash::hash()
    {
         cout<<"Give size:";
         scanf("%d",&size);
         class hash * table[size];
         for(i=0;i<size;i++)
              table[i]=NULL;
    }
    void hash::insert(int a)
    {
           if(a[0]==NULL)
                   cout<<"it's null"<<endl;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The class hash has a private data member that is an array of hash pointers?

    Don't you mean is has a private data member that is an array of linked lists?

    In any case, you should be using a vector<list<T> > for the array of pointers.

    In C++ you should not bs using arrays at all unless you can write down on paper the precise technical reason why a vector won't work.

    Comment

    • AlarV
      New Member
      • Dec 2008
      • 37

      #3
      Well it has a private data member that is an array of linked lists, but these lists are also a pointer of class hash. We haven't been taught vectors. I have no idea what it is :(

      Is there another way to do it?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Learn them yourself vectors are just like arrays but with all the hassle of managing the memory taken care of for you.

        Line 10 of your code, the keyword class is not required.

        Line 23 of your code, you declare a local variable table masking the class member variable table. You then initialise the local copy and when the function exits the local copy of table is automatically deleted for you leaving the class member variable table uninitialised.

        Comment

        • AlarV
          New Member
          • Dec 2008
          • 37

          #5
          Thanx for the replies everyone!
          At last I figured out what to do.. I wanted an array of pointers. So this means class hash **table; and the program works!
          When I call the constructor the table changes and can be used throughout the program.

          Comment

          Working...