473,749 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I have access to private variables in classes?

37 New Member
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 2733
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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
2599
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 schema { private:
11
4613
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
6
4751
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 appreciated. Thanks in advance
4
1399
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 web clients attempt to use the application at the same time. A common error that gets returned is "sqlcommand is currently busy open, fetching". I can't understand why the users might be using the same instance of my connection, but this error...
17
5533
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 too much - that it's bad programming practice. Is there an easy way to deal with this? I would like to do things in the "Best Practices" way but at the same time I don't want to make a federal case out of it. This comes up over and over...
9
1861
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 to the order */ }
37
2601
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
19624
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 Class Parent '*** HOW TO DECLARE IT *** Dim Age As String = "1/1/2000"
2
3988
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 same name as the parent context, but I want direct access to the parent context's variable. E.g. I would like to be able to do this... function Point()
7
2184
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
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.