473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do I have access to private variables in classes?

37
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:
Expand|Select|Wrap|Line Numbers
  1. #ifndef hashdef
  2. #define hashdef
  3.  
  4. using namespace std;
  5.  
  6. class hash
  7. {
  8.    int i;
  9.    int size;
  10.    class hash *table[ ];
  11.    public:
  12.       hash();
  13.       void insert(int a);
  14.       void deletekey(int key);
  15.       bool search(int a);
  16.       void destroy();
  17. };
  18.  
  19. hash::hash()
  20. {
  21.      cout<<"Give size:";
  22.      scanf("%d",&size);
  23.      class hash * table[size];
  24.      for(i=0;i<size;i++)
  25.           table[i]=NULL;
  26. }
  27. void hash::insert(int a)
  28. {
  29.        if(a[0]==NULL)
  30.                cout<<"it's null"<<endl;
  31. }
  32.  
Nov 29 '09 #1
4 2721
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 29 '09 #2
AlarV
37
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?
Nov 29 '09 #3
Banfa
9,065 Expert Mod 8TB
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.
Nov 29 '09 #4
AlarV
37
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.
Nov 29 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: diadia | last post by:
when i write two classes as follow . compiler tell me that i can't access private number from other class but if types of two classes are the same it don't have error message ? why? class...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
4
by: Heath Kelly | last post by:
I need advice on correct usage of ADO.NET in an ASP.Net environment. I have an ASP.Net application that accesses data through a referenced class library. Things start to break down when multiple...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
2
by: Rob Long | last post by:
Hi there Is there any way to access private variables directly from within a priviliged function? I have a situation where the priviliged function's execution context contains variables of the...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.