How to use 2 different class variables in a function
ok so this isnt an actual concern i have for my game or anything i just got to thinking about it and am curious. So lets say i have a class
1 2 3 4 5 6 7 8 9
|
class testOne
{
public:
testOne();
~testOne();
private:
int num1;
};
|
and i have another class that does not inherit from testOne
1 2 3 4 5 6 7 8 9
|
class testTwo
{
public:
testTwo();
~testTwo();
private:
int num2;
};
|
so i have a function that inherits from testOne
void testOne::function()
{
num2 = 800;
cout << num2 << endl;
}
how would i use the other classes variables in this function without having to make an object?
That function is a member function. It does no inheriting at all.
You can make the function a friend.
1 2 3 4 5 6
|
class testTwo{
public:
//...
friend void testeOne::function(testTwo&);
//...
};
|
Now it has access to the private and protected members. Don't get carried away with
friend
s, though. They break encapsulation.
Last edited on
ok thanks.
I would just make testTwo member functions to set and get the member vars.
Topic archived. No new replies allowed.