C++ getters/setters coding style

前端 未结 8 1494
小蘑菇
小蘑菇 2020-11-27 10:41

I have been programming in C# for a while and now I want to brush up on my C++ skills.

Having the class:

class Foo
{
    const std::string& name         


        
8条回答
  •  无人及你
    2020-11-27 11:39

    I think the C++11 approach would be more like this now.

    #include 
    #include 
    #include 
    
    template
    class LambdaSetter {
    public:
        LambdaSetter() :
            getter([&]() -> T { return m_value; }),
            setter([&](T value) { m_value = value; }),
            m_value()
        {}
    
        T operator()() { return getter(); }
        void operator()(T value) { setter(value); }
    
        LambdaSetter operator=(T rhs)
        {
            setter(rhs);
            return *this;
        }
    
        T operator=(LambdaSetter rhs)
        {
            return rhs.getter();
        }
    
        operator T()
        { 
            return getter();
        }
    
    
        void SetGetter(std::function func) { getter = func; }
        void SetSetter(std::function func) { setter = func; }
    
        T& GetRawData() { return m_value; }
    
    private:
        T m_value;
        std::function getter;
        std::function setter;
    
        template 
        friend std::ostream & operator<<(std::ostream &os, const LambdaSetter& p);
    
        template 
        friend std::istream & operator>>(std::istream &is, const LambdaSetter& p);
    };
    
    template 
    std::ostream & operator<<(std::ostream &os, const LambdaSetter& p)
    {
        os << p.getter();
        return os;
    }
    
    template 
    std::istream & operator>>(std::istream &is, const LambdaSetter& p)
    {
        TT value;
        is >> value;
        p.setter(value);
        return is;
    }
    
    
    class foo {
    public:
        foo()
        {
            myString.SetGetter([&]() -> std::string { 
                myString.GetRawData() = "Hello";
                return myString.GetRawData();
            });
            myString2.SetSetter([&](std::string value) -> void { 
                myString2.GetRawData() = (value + "!"); 
            });
        }
    
    
        LambdaSetter myString;
        LambdaSetter myString2;
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        foo f;
        std::string hi = f.myString;
    
        f.myString2 = "world";
    
        std::cout << hi << " " << f.myString2 << std::endl;
    
        std::cin >> f.myString2;
    
        std::cout << hi << " " << f.myString2 << std::endl;
    
        return 0;
    }
    

    I tested this in Visual Studio 2013. Unfortunately in order to use the underlying storage inside the LambdaSetter I needed to provide a "GetRawData" public accessor which can lead to broken encapsulation, but you can either leave it out and provide your own storage container for T or just ensure that the only time you use "GetRawData" is when you are writing a custom getter/setter method.

提交回复
热议问题