Should a class use its private members directly or publicly disclose member functions?

Well, I'm not so sure about that.

I have a class like

    class Object
    {
        int internalValue;
        struct SomeStructType
        {
            int superInternalValue;
        } someStruct;

    public:
        int getInternalValue ()
        {
            return internalValue;
        }

        int getSuperInternalValue ()
        {
            return someStruct.superInternalValue;
        }

        void printThatInternalValue ()
        {
            // Its seems pretty clear we should just access
            // the private value directly and not use the public getter
            printf ("% d", internalValue);
        }

        void printThatSuperInternalValue ()
        {
            // Now here the access pattern is getting more complicated.
            // Shouldn't we just call the public access function
            // getSuperInternalValue () instead, even though we are inside the class?
            printf ("% d", someStruct.superInternalValue);
        }
    };

So, for operations of the INTERNAL class, it can be used directly someStruct.superInternalValue, but instead, the public public getter function is easier to use getSuperInternalValue().

The only drawback I see is trying to change superInternalValueusing getter and it will give you a copy that is clearly not what you want in this case. But for read access, should the public getter function be used inside the class?

+3
source share
5 answers

, . , , .

+3

, , - , - , , ; , , - .

, , , , .

+1

, , 90% , , , , , . someStruct::SuperInternalValue getSuperInternalValue, , someStruct , Object.

, someStruct, Object - ( , ), someStruct ( ). , Object::printThatSuperInternalValue someStruct superInternalValue.

, "" "", Object, someStruct. , , , OO - . , encapsulation , , , , , .

class Object { 
    int internalValue;
public:
    class someStruct { 
        int superInternalValue;
    public:
        someStruct(int v) : superInternalValue(v) {}
        someStruct &operator=(someStruct const &n) { 
             superInternalValue = n.superInternalValue;
             return *this; 
        }
        friend ostream &operator<<(ostream &os, someStruct const &s) {
            return os << s.superInternalValue;
        }
    };
    friend ostream &operator<<(ostream &os, Object const &o) { 
        return os << internalValue;
    }
};

someStruct ( int, ), Object /touch superInternalValue. , Object someStruct, someStruct , Object gory , someStruct.

+1

, , , .

While an accessor can initially act as a simple shell that does nothing more than get the value of a private member, future access modification can change this behavior, so any other method that accesses a member no longer has a consistent look at its value .

0
source

It all depends. I do not think that in general you should use getter inside the class. For instance,

int MyClass::getFoo() const
{
   int retval(0);
   m_myMutex.lock();
   retval = m_foo;
   m_myMutex.unlock();
   return retval;
}

You probably aren't using such a getter inside MyClass.

0
source

Source: https://habr.com/ru/post/1733456/


All Articles