The question is simple: Is it unnecessary to call getters inside setters to have access to an object's member variables? Suppose that all the getters are inline
d and return const
references to members.
As an example:
class Foo
{
public:
inline const std::uint32_t& getValue( ) const noexcept
{
return m_value;
}
inline const std::uint32_t& getBar( ) const noexcept
{
return m_bar;
}
void setValue( const std::uint32_t value )
{
m_value = value * getBar() * 3; // vs m_value = value * m_bar * 3;
}
private:
std::uint32_t m_value;
std::uint32_t m_bar;
};
Which one is more idiomatic? I think there should be no difference in the generated code by the compiler. But in terms of readability, they're a bit different. What could be the benefits of using getters instead of directly typing e.g. m_bar
?
Code in the class always has full acess to the internal data members (and member functions too). So it is not necessary. My thoughts on if you should do it
note I dont say whats 'best', just things to take into account