Is it possible to access data members from other objects within an object's function? (and more)

I can't think of a good example, and I don't think I can really elaborate further either.

Also, how do I access another variable altogether (from within a member function)? A global constant or variable, for example.

_________________________________________________________________________________

More:

I want to declare some global constants and variables, but I don't want to do it all in main.cpp. Is there any way I can declare them in another file?

____________________________


With SDL, I want to initialise graphics (again) without having to clutter up my main file. Is there any way of declaring them in some sort of object? (eg. shoving them in a graphics class)
1) If it's a static member, you could access it like this: classname::variable. If not, you'll need an object to be able to access the class members.

2)You could access it normally, if there are no variables or functions with the same name in this class. If there are any, precede it with ::. You probably should declare the global variables or functions in a header file, for example: extern int x;. Then define them in a source file.

3) See point 2.

4)Why not?
1) I'll try and give an example:

1
2
3
4
5
void Graphics::Draw()
{
    cout << player.x << player.y;
    cout << enemy.x << enemy.y; //ok, silly example, I'm just showing what I mean
}


Alternatively, is there a simple way (other than copy-pasting) of having the same function for different classes? (So I could have player.Draw, then enemy.Draw) Would inheritance be the way to go here?

2) So that's what extern does! Great, thanks!

4) Because I don't know enough about SDL ;)

In your example, is player and enemy objects declared in Graphics class ? If yes, then you can access them like that. If no, that won't work.
No, they would have Player and Enemy classes.

How would I access them at all? Just pass co-ordinates, frame, direciton etc in the argument?

Also, if I have an object declared inside another object, how do I access it's variables / functions?

eg.


1
2
3
4
5
6
7
class Player
{
public:

    Gun gun();

}


Can I just say player.gun.Shoot(); or do I have to do that in a player function:

1
2
3
4
void Player::Shoot()
{
    gun.shoot();
}


____________________________________________________

Also, as before, if I wanted a Draw function, separate from any Graphics class, and I want to access the same function using different objects: player.Draw; enemy.Draw; would I just use inheritance, so that the Player and Enemy classes are derived from an Entity class?
Last edited on
This is wrong:
Gun gun();

This is right:
Gun gun;

Can I just say player.gun.Shoot(); ?

Yes, as long as gun and Shoot() are public.

Also, as before, if I wanted a Draw function, separate from any Graphics class, and I want to access the same function using different objects: player.Draw; enemy.Draw; would I just use inheritance, so that the Player and Enemy classes are derived from an Entity class?

I'm afraid I don't fully understand what you mean. Does this accomplish what you want ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Entity
{
public:
	virtual void Draw();
};

class Player : public Entity
{
public:
	void Draw(); // this overrides Draw() function in class Entity	
};

class Enemy : public Entity
{
public:
	void Draw(); // also overrides 
};

Draw(Entity* entity) // you can pass player or enemy objects
{
	entity.Draw(); // this will call the respective functions of the objects passed
}
I added the brackets because normally I would construct a Gun object with parameters.


What I meant was (for a rubbish example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Entity
{
public:
    void Draw();
};

class Player : public Entity
{
public:
    void Input();
};

class Enemy : public Entity
{
public:
    void Attack();
};


So that I could call:

1
2
player.Draw();
enemy.Draw();


as part of main.
Last edited on
Topic archived. No new replies allowed.