-1

Is there a best practice for something like this?

(Example in C++)

class A {
    public:
        int usefulParameter;
        std::vector<B*> bContainer;
};
class B {
    public:
        void needsUsefulParameter(int u);
};

Are there any good ways for objects of class B to access fields from class A, even though they may not be closely related? In this case, A "has a" B, but not necessarily "owns a".

Does the function purpose lie in the responsibility of A, or does it lie with B?

To use the field of A and field of B, it could happen like

class A {
    public:
        int usefulParameter;
        void needsUsefulParameter(B* b);
};

where A has the responsibility of fulfilling the action required. It could also happen on the B side

class B {
    public:
        int b_field;
        void needsUsefulParameter(A* a);
};

Example: A game has a world state. Players interact with the world, and their actions depend on the state of the world. For instance, we want to compare currency, but the value of the currency changes with the season. In the summertime (game state would hold this), ice cream is a high-value currency. In the winter, wool is a high-value currency.

In c++, it would be nice to overload the <, > <=, >=, == operators.

class Currency{
    string currencyobject;
}
Currency w("wool");
Currency w("icecream");

cout << wool < icecream << endl;

But should the world-object be responsible for calculating that comparison, or should the currencies pull in fields from the world-object?

Sorry I'm not sure what the best way is to word this. Part of my trouble is finding the right words to search for. What's the noun for a super-object that is an aggregation of sub-objects? etc, etc, etc.

  • "In c++, it would be nice to overload the <, > <=, >=, == operators." ... why? How is a currency "less than" another? A book is not "less than" another book just because it is smaller. The question you're asking is "is more valuable than". That's not the same as "less than". And since your design requires that this question must take into account non-local state, whatever function answers that question ought to be given access to the non-local state. – Nicol Bolas Jul 18 '18 at 20:59
1

Are there any good ways for objects of class B to access fields from class A, even though they may not be closely related?

If they're not closely related, then B has no right to access fields from A. If B objects are not logically related to some A, then B's member operations should not be accessing A.

And if that relationship exists (or conditionally exists), then it's best to make this relationship a formal part of the objects by having B store an actual pointer/reference/etc to the A that it is related to.

But should the world-object be responsible for calculating that comparison, or should the currencies pull in fields from the world-object?

You're asking the wrong question, at the wrong time.

The first question you need to take into account is, "when deciding the valuation of currency, what state do I need to take into account?" When you have a complete answer to that question, then you can decide what object would be responsible for computing it.

Or if there's an object responsible for it at all; there's no reason why the CalcCurrencyValuation function needs to be a member of an object. Currency valuation is a process, an algorithm, not an object. It uses data provided by objects, but the concept isn't part of the "world". It's just a thing that can be done given a set of state.

Overall, this kind of design speaks to an over-investment in OOP thinking. Your design seems to be trying to put everything into some kind of member function, even if it doesn't really make sense to do so. This kind of design leads to fat interfaces (bubbling functions up to this "world" class) and the like. This also leads to problems if your design suddenly requires that other state be taken into account in order to compute a currency's valuation. You now have to make sure that state is part of this "world" class.

It is a function; treat it as such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.