how to access hash table from all other classes

I cant post code since it is a lot, but basically i have a hash table class, created in hash.cpp and hash.h and then a create the table in the main.cpp using hashtable hash (so the constructor) but i need access to hash in other classes, is there a way that is possible?
If you need other objects to access your hash object, you need to pass the hash object to them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Example program
#include <iostream>

class hashtable {
  public:
    int value; // just for demonstration
};

class other {
  public:
    other(hashtable& hash)
    : hash(hash)
    {
        
    }
    
    void do_stuff()
    {
        hash.value += 5;   
    }
    
    hashtable& hash;
};

int main()
{
    hashtable hash;
    hash.value = 42;
    
    other obj(hash);
    
    std::cout << "value before doing stuff: " << hash.value << '\n';
    
    obj.do_stuff();
    
    std::cout << "value after doing stuff: " << hash.value << '\n';
}
value before doing stuff: 42
value after doing stuff: 47

It doesn't need to be a reference member of the class, you could also just pass hash into a function of the other class (pass by reference).

If this doesn't answer your question, you might need to be more specific in what the issue is.
Last edited on
I tried your code and it didnt seem to help me out, maybe because it was done over different headers and cpp files?. What I want to do is have my hash table class which i have in the hashtable cpp, then the main.cpp creates the table only, using the constructor. And then I want to have a player class, which will allow a player to access the contents of the table, to print it etc.

I have no code for the player class yet, but all it needs to do is be able to access the hash object that is created in the main.cpp.

If this still doesnt explain what i am meaning, i can try again, sometimes i am terrible at explaining myself, please just let me know :)

EDIT: I apologise, i may have been spelling something wrong, your code may work, i will update. again apologies
Last edited on
If the logic inside your Player class needs to access the contents of the hashtable object, then it needs to know what a hashtable is. As far as header file stuff goes, you would need to make sure your Player class file #includes your hashtable header file.

You would still do the same thing,
1
2
3
4
5
6
7
8
9
#include "hashtable.h" // or whatever
#include "Player.h"
int main()
{
    hashtable hash;

    Player player;
    player.access_contents_of(hash);
}


In Player.h:
1
2
3
4
5
6
7
8
9
10
#ifndef PLAYER_H_
#define PLAYER_H_

#include "hashtable.h"
class Player {
  public:
    void access_contents_of(hashtable& table);
};

#endif // PLAYER_H_ 


In player.cpp: (presumably)
1
2
3
4
5
6
#include "Player.h"

void Player::access_contents_of(hashtable& table)
{
    /* modify table */
}


Dealing with multiple files only adds to the complexity here. You don't actually need to use multiple files. You can focus on getting the logic right, and then break up the files if you wish.
Last edited on
Thank you, ive passed in the hash table to the function itself and have it working now. Thank you for the help, much appreciated.
Topic archived. No new replies allowed.