glaziko
glaziko

Reputation: 81

In general, is there a there a way to access a Child class from another child class when they both share the same parent class?

So let's consider we have a Parent class

class Parent
{
};

I have then two child class

class Child1 : public Parent
{
 void AccessFunction();
}

and

class Child2 : public Parent
{
void FunctionBeingAccessed();
}

where

void Child1::AccessFunction()
{
  FunctionBeingAccessed();
}

Is it possible?

EDIT: Thanks for the answers !

So the reason i am asking this is because i have a third class GrandChild, And i was searching for a way to Make it a Child of ALL the Child classes of the Parent Class, despite them being different.

Is there a way to do so?

EDIT2:

Better explanation of my problem.

diagram

So i have a player, item and level class who are childs of the 3dmodel class. And i would like to make the collision class, a child of them. So do i need to use virtual inheritance

Upvotes: 1

Views: 90

Answers (2)

Carlton
Carlton

Reputation: 4297

You can declare Child1 a friend of Child2, then objects of type Child1 can access private/protected methods of Child2.

Upvotes: 0

TRids
TRids

Reputation: 38

I believe it is possible, but you have to set the protection level of FunctionBeingAccessed(); to public. The default protection level for classes is private, and therefore your code won't work.

Change void FunctionBeingAccessed(); to this:

class Child2 : public Parent
{
public:
void FunctionBeingAccessed();
}

That should work.

Upvotes: 1

Related Questions