A question for OOP experts. - page 35

 
Реter Konow:

How are cross-links between descendant classes that have a common root somewhere deep in the hierarchy established?

For example: Class F and class Z are the end links of an inheritance chain with a common root in the hierarchy, class A.

How can one get data or methods of class Z from class F?

You can't. There is no such thing. But there is no need to.

After creating objects you can pass a pointer from one object to the second and a pointer from the second object to the first.

Although, if we are speaking specifically about the class, you can address any object through two colons. But what for do you need it? It is needed sometimes, of course, but it is an extremely minor OOP feature.

 
Dmitry Fedoseev:

No way. There's no such thing. But you don't need to.

After creating objects, you can pass a pointer of one object to the second and a pointer of the second to the first.

Although, if we are speaking specifically about the class, you can refer to anything through two colons. But what for do you need it?

It all depends on what tasks the F class is solving. There may be a situation where data or methods used in a third-party inheritance chain may come in handy.
 

Example: Class F stores Colour and Length methods, and Class Z stores Sound and Width methods.

An Object appears that has two properties, Length and Width, but they are calculated by different F and Z classes. What to do? Create a separate class with repetition of these methods, or how do I access them? (With a four dots?).

 
Реter Konow:

Example: Class F stores Colour and Length methods, and Class Z stores Sound and Width methods.

An Object appears that has two properties, Length and Width, but they are calculated by different F and Z classes. What to do? Create a separate class with repetition of these methods, or how do I access them? (With a four dots?).

You could create a new class and include the F and Z classes in it.

class F{
};

class Z{
};

class Y{
   public:
   F f;
   Z z;
}
 
Dmitry Fedoseev:

You can create a new class and include classes F and Z in it.

A bowing class? Got it. Thanks.
 
Dmitry Fedoseev:

You can create a new class and include classes F and Z in it.

Or you can make a method in the class where you want the data of the other class to get a pointer to the required class.

 
Реter Konow:
A class "bow"? Got it. Thanks.

You can just get a pointer to the class you want, and use the pointer to get a class object. But you have to be careful not to get a pointer to an empty or new class object.

 
Artyom Trishkin:

You can simply get a pointer to the desired class, and use the pointer to get a class object. But you have to be careful not to get a pointer to an empty class object.

Subtleties. I see. Hierarchy of inheritance is convenient when data is clearly classifiable, predefined and unambiguous. As objects become more complex, combinations of methods and properties not anticipated in classes may appear, which requires construction of "bindings" - i.e. cross-links between classes. This is not always convenient and may break the beautiful distribution scheme and force it to be revised.
 
Реter Konow:
A "bowing" class? I see. oops.

In the example

class F{
};

class Z{
};

class Y{
   public:
   F f;
   Z z;
}

Class Y contains variables with types of classes F and Z.

But if you already have objects of classes F and Z created and used somewhere, you will not be able to access them in object Y. There will be two new objects of classes F and Z in object Y.

And to get access to the objects of classes F and Z created earlier and already used, you need to get references to objects F and Z in class Y - then class Y can operate with those data, which are already written in F and Z, instead of using new and pristine F and Z.

But if you create such an object, then F and Z in it will give access to classes F and Z, and you have to fill them only via class Y - again, give access to objects F and Z from it, get a pointer to the object you need and work with it by the pointer.

 
Реter Konow:
Subtleties. I see. Hierarchy of inheritance is convenient when the data is clearly classifiable, predefined and unambiguous. As objects get more complex, combinations of methods and properties not intended for classes may appear and this requires creation of "bindings" - i.e. cross-links between classes. This is not always convenient and may break the beautiful distribution scheme and force it to be revised.

You don't need to break anything - you just need to add a method to the class that gives a pointer to the required object of another class.

In the recent articles I've passed pointers to previously created and working and having accumulated data collectionclass objects and the current account object in trade objects in this way. And the trade class handles them as if it had always had them.