Imagine
Imagine

Reputation: 95

C++ how to access class member function between instances

Is there a way to access a specific Class instance's member function within a member function definition? Let me clarify what I mean in a pseudo code below.

Thanks!

// A Class called Dog
class Dog
{
public:
void eat();
void drink();
//... More code here
};

void Dog::eat()
{
//How do I always access dog1.drink() here, regardless of which instance calls it?
}
//... More code here
// Instances of Dog
Dog dog1, dog2;

Upvotes: 0

Views: 90

Answers (1)

If you want to make dog1 drink, you simply call:

dog1.drink();

It makes no difference whether you write this inside a member function of Dog or not. There is no need to overthink things here.

Note: Like any other use of a global variable, the global variable has to be declared before the code that uses it.

Upvotes: 3

Related Questions