Using same class instance across multiple files

How to use an instance of a class across multiple files?

This is by far, the hardest problem i have ever came across in any programming languages.

I am literally going insane over this because whenever i wanna use OOP i always stumble upon this particular problem and give up on my project.

So, how do you do it?

Here's what I've got so far.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

#include <Graphics.h>
#include <Player.h>

extern Player Playerz(1, 1);

int main(){
  Graphics GUI;
  GUI.Show_Menu();
  return 0;
}


Player.h

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

#ifndef PLAYER_H
#define PLAYER_H

typedef unsigned short int Grid_Position;

class Player{
    public:
        Player(Grid_Position, Grid_Position);
        void MoveX(Grid_Position);
        void MoveY(Grid_Position);
        Grid_Position ShowX();
        Grid_Position ShowY();
        const char Show_Skin();
        ~Player();
    private:
        Grid_Position X;
        Grid_Position Y;
        const char Skin = '#';
};

#endif // PLAYER_H


Player.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Player.h"

Player::Player(Grid_Position X, Grid_Position Y){
    Player::X = X;
    Player::Y = Y;
}

Grid_Position Player::ShowX(){
    return Player::X;
}

Grid_Position Player::ShowY(){
    return Player::Y;
}

const char Player::Show_Skin(){
    return Player::Skin;
}

Player::~Player()
{
    //dtor
}


Graphics.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef GRAPHICS_H
#define GRAPHICS_H

#include <main.cpp>
#include <iostream>

class Graphics{
    public:
        Graphics();
        void Show_Menu();
        ~Graphics();
    private:
};

#endif // GRAPHICS_H 


Graphics.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Graphics.h"

Graphics::Graphics(){
    //ctor
}

void Graphics::Show_Menu(){
    std::cout << Playerz.ShowX();
}

Graphics::~Graphics()
{
    //dtor
}
Last edited on
A solution on top of my head.

Declare a static pointer in player.h
 
static Player* pPlayerz;

Define it in player.cpp
1
2
3
#include <player.h>
Player* pPlayerz = NULL;
//...// 

In main.h
1
2
3
4
5
6
7
8
9
//...//
#include <Player.h>

Player Playerz(1, 1);
int main()
{
   pPlayerz = &Playerz;
   //...//
}


And call pPlayerz whenever you need something from Playerz.

There is probably a better and safer way to do this however, since if pPlayerz is NULL because main() failed to initialize it, you will segfault.

You can also set the other classes to store a Player* and set the constructor to take a pointer to a Player instance.

1
2
3
4
5
6
7
8
class Graphics
{
    public:
    Graphics(Player*);
    //...//
    private:
    Player*  pPlayerz;
};


1
2
3
4
5
6
7
#include "Graphics.h"

Graphics::Graphics(Player* inputPtr)
{
    //...//
    pPlayers = inputPtr;
}


1
2
3
4
5
6
int main()
{
    Player Playerz(1,1)
    Graphics GUI(&Playerz);
    //...//
}


Then call anything you need through the pointer. This is much safer, since you can make an if statement to see if the pPlayerz is pointing to a valid instance.

Last edited on
It was a stupid logic error.

I don't have to find a way to use the same class instance across multiple files, i can simply give my method his class instance's value from main.cpp and display it.

I hope it makes sense cause it's hard to explain.
Last edited on
Topic archived. No new replies allowed.