I had an idea for a feature for C++, and I was wondering if it was possible to create.
Let's say I want a private variable in 'MyClass' to be accessible only by two functions, the public getter and setter. That is, if another public or private function of MyClass tries to get or change the value of my super-private variable, I will get a compile error. However, the getter and setter behave normally.
Any ideas?
Edit 1: A use case is having the getter/setter perform error checking or other form of logic. I wouldn't want even the class itself touching the variable directly.
Edit 2: Something like this then:
template <class T> class State{ private: T state; public: State() { state = 0; } T getState() { return state; } void setState(T state) { this->state = state; } };
And then any class can inherit it and access 'state' only through the getter/setter. Of course, the class is useless without modifying the getter and setter according to your desired logic.
If you want private variables in c, there are a number of techniques that can approximate a private variable, but the C language actually doesn't have a "protection" concept that extends to private, public, protected (as C++ does).
To access private variables of parent class in subclass you can use protected or add getters and setters to private variables in parent class.. Show activity on this post. You can't access directly any private variables of a class from outside directly. You can access private member's using getter and setter.
No class can access private variables. Not even subclasses. Only subclasses can access protected variables. All classes can access public variables.
private (C++) When preceding the name of a base class, the private keyword specifies that the public and protected members of the base class are private members of the derived class. Default access of members in a class is private.
The granularity of accessibility in C++ is the class.
So if you need to make a variable accessible to only two methods you need to move the variable and the two methods into a separate class, dedicated to maintaining privacy..
You could wrap the variable in a class and make it private there and a const T&
getter. Than you declare the get and set member functions of the containing class as friends to that wrapper. Now you keep the wrapper class as a member in your original class. That should achieve what you want albeit it looks hard to maintain and not very useful.
So here is some dummy implementation that shows how this would work (Note that the whole new VeryPrivateWrapper
business is just a wacky way around declarations, a unique_ptr
would be more helpful):
class VeryPrivateWrapper; class Original { VeryPrivateWrapper* m_wrapper; public: Original(); // imagine that I remembered the rule of three here void set(int); void other(); }; // make this a template for more fun class VeryPrivateWrapper { int m; public: const int& get() const { return m; } // !!! // the important bit // !!! friend void Original::set(int); }; Original::Original() : m_wrapper(new VeryPrivateWrapper) {} void Original::set(int i) { m_wrapper->m = i; } void Original::other() { // this borks as we would like // m_wrapper->m = 23; } int main() { Original o; o.set(3); return 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With