Search code examples
c++classmember-functionsclass-membersmember-variables

C++ - Internally, when defining a class's member functions, should the member variable name or its getter function be used?


I apologize if the question title doesn't effectively (or at all) reflect my actual question; this is my first time asking or answering on stackoverflow and may have goofed...

Anyway, my (elaborated) question is this:

When defining public member functions in a class that require use of private member variables, each of which (the private vars) having public member "getter" functions (please no answers saying "lose the getters/setters...", I'm using them), would it be better -- actually "better", like which makes the class more "extensible" between C++ compilers and which option allows for more "forward compatibility" -- to use this->m_variable OR to use this->getm_variable()?

I've included the following code example (creating a class named Family) from the project I am currently working on to try and clarify what exactly I mean:

Snippet of Family.h

private:
    Person* children; // array of Person objects - Person is a member struct
    size_t numChildren;

public:
    enum gender { NONE = 0, MALE, FEMALE }
    size_t countChildren() { return numChildren; }
    size_t countChildren(gender g);
    Person* getChild() { return children; }

Snippet of Family.cpp

// this is the specific function example:
size_t Family::countChildren(gender g) {
    for (size_t i = 0; i < this->numChildren; i++) {
        ...CODE TO COUNT CHILDREN OF SPECIFIED GENDER...
    }
    return # of children with gender g   // pseudocode, obviously...
}

So, now for the question changed for the example:

In the definition of the countChildren(gender g) function where I have i < this->numChildren;would it be better (in terms of the metrics I stated earlier) to use i < this->countChildren();? Similarly, when iterating through the children array for the same for loop, would it be better to define each Person* in the current iteration using *(this->children + i) OR to use *(this->getChild() + i);?

Thank you in advance for any help and I apologize if this question is too "discussion-based" for stackoverflow, as I mentioned earlier, this is my first time asking a question.


Solution

  • In an ideal world, you'd always refer to logical properties internally with getters. This only really matters when said properties are meant to be exposed publicly; if you find yourself writing a private getter that's only used for an implementation detail, stop coding and get a coffee, then think about what you're doing.

    Why use getters internally? For the same reasons you would use them anywhere else; they're more resilient to changes in your class' representation. Except this time it's not just other code getting the benefits, it's this class too.

    By extension, this makes it easier to extend classes. What if you want countChildren to be pure virtual (or abstract, if you prefer Java), and let different subclasses decide how to count children? (Doesn't mean you always should, but you can. That's a separate issue, though.)

    And don't say "performance hit from function calls". That'll probably be optimized out, especially if the getter is inline or non-virtual.

    In a practical world, whether or not this really matters depends on how many other people will use your code, or if it's known that your class' data representation is unlikely to change (which is sometimes actually a reasonable expectation, like for mathematical vectors).