I just wondered if I could bypass using getters if I just allowed a const reference variable, as follows
#include <string>
class cTest
{
    private:
        int m_i;
        std::string m_str;
    public:
        const int & i;
        const std::string & str;
    cTest(void)
    : i(m_i)
    , str(m_str)
    {}
};
int main(int argc, char *argv[])
{
    cTest o;
    int i = o.i; // works
    o.i += 5; // fails
    o.str.clear(); // fails
    return 0;
}
I wonder why people do not seem to do this at all. Is there some severe disadvantage I am missing? Please contribute to the list of advantages and disadvantages, and correct them if necessary.
Advantages:
- There is no overhead through calls of getter functions.
- The program size is decreased because there are less functions.
- I can still modify the internals of the class, the reference variables provide a layer of abstraction.
Disadvantages:
- Instead of getter functions, I have a bunch of references. This increases the object size.
- Using const_cast, people can mess up private members, but these people are mischievous, right?
 
     
     
     
    