Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in C++ to create 'super private' variables?

Tags:

c++

oop

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.

like image 915
Assaf Muller Avatar asked Sep 28 '11 11:09

Assaf Muller


People also ask

Are there private variables in C?

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).

Can you access private variables in a subclass?

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.

Can subclasses access private variables C++?

No class can access private variables. Not even subclasses. Only subclasses can access protected variables. All classes can access public variables.

What is private keyword in C?

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.


2 Answers

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..

like image 58
David Heffernan Avatar answered Sep 19 '22 09:09

David Heffernan


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; } 
like image 32
pmr Avatar answered Sep 19 '22 09:09

pmr