Reputation: 941
Stupid question probably, but I couldn't find the answer (even here).
I have split all my classes in separate files (cpp+h). I have methods like getValue()
and setValue()
. I have class called Player (this is basically the database with whole number of variables). I created an object called player (Player *player = new Player;)
. Now I need to access THIS object from any other class (that separated in another file). The problem is that I cannot even access getValue() / setValue()
methods.
What I need is something like in Delphi From1.player.item[0]=3
access from form 2, where the player is recorded.
UPD:
Here's my code:
Player.cpp
#include "player.h"
#include "gameform.h"
Player::Player()
{
}
void Player::check(){
//I should be able to check if player dead or not from my Battle class
}
player.h
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
public:
Player();
void check();
};
#endif // PLAYER_H
Battle.cpp
#include "battle.h"
#include "player.h"
#include "game.h"
Battle::Battle()
{
}
void Battle::hit(){
//if I hit I should check am I dead yet
player.check();
}
That's how Player declared in Game class (now):
Player *player = new Player;
Outside of any functions, just in class.
Where player is object, created in Game class. All things are public.
*I've tried even creating object in main.cpp (both in and out of main() function), and nothing working, weird =/
This is github "temp" branch, compiling and working. But how to access player? :)
https://github.com/ewancoder/game/tree/temp
UPD: Another stupid question: If I want that in my class 1 function is responsible for opening file and another - for editing and closing file, how can I do that if one cannot read vars from another?
Upvotes: 0
Views: 4884
Reputation: 10868
Your question is a little hard to follow, and some of the answers reflect that. However, in my view what you are running into is actually a critically important part of your system design. There are many strategies, and many considerations, but basically it's up to you to design the paths by which objects can access the contents of other objects in your running program.
Some common techniques include Global variables
extern Player *PlayerOne;
PlayerOne->item[0]=3;
Static variables (or collections) in the class.
class Player {
public:
Player* Players;
}
Player::Players[0]->item[0]=3;
Member variables.
Board.Add(new Player(Board));
Board.m_player->item[0]=3;
The variations are endless. You'll need to be more specific if you want specific code.
Upvotes: 0
Reputation: 5518
You need to add parenthesis to the end of the statement and use the ->
operator, not the .
operator: player->check()
This is because it is a pointer to an object Player *player
and not an object itself Player player
Upvotes: 0
Reputation: 8209
I'm not sure what you want, but if you have a class like this:
a.hpp
Class A {
public:
void foo();
};
a.cpp
#include "a.hpp"
void A::foo() {}
You can use it like this:
b.hpp
#include "a.hpp"
class B {
public:
void stuff(A& a);
};
b.cpp
#include "b.hpp"
void B::stuff(A& a) { a.stuff(); }
Upvotes: 1
Reputation: 893
You need to give the other object the reference of the first. Either direct in the constructor or later via a method.
Or alternativly you could store player in a global variable and access it via the global variable.
The problem is that the methods are bound to the specific instance of the object - and so you need that instance to call them
Upvotes: 0