Hi Jim,

 

a way to not change too much would perhaps be:

 

class C {  
public:
    void printi() { cout << "Value: " << i.var << endl; }

private:
    ProtectedData<int, &C::printi> i;
};


With ProtectedData being a template class that creates an private int i.var and is

friends with member function C::printi.

 

Only for friends it provides access to i.var.

 

Not sure, if you would be happy with such a solution or what would be missing (if at all) to write a

ProtectedData template (member function pointers can be template arguments, member functions can be friends,

but perhaps both concepts do not work together, as for a friend function you have to specify the declaration,

whereas for a class the class name is enough? Perhaps ProtectedData would use an intermediate class).

 

 

Best,

Sebastian


 

-----Ursprüngliche Nachricht-----
Von: Smith, Jim via Std-Proposals <std-proposals@lists.isocpp.org>
Gesendet: Mo 07.11.2022 21:17
Betreff: [std-proposals] Namespace with access specifiers in class interface
An: std-proposals@lists.isocpp.org;
CC: Smith, Jim <jim.smith@oursmithfamilyonline.com>;
Hi,

Sometimes I'd like to hide data from a subset of class methods while still allowing other methods to access them. Also it would be helpful to set read/write access on class data for certain methods.

Allowing namespaces with access specifiers in class interface would look like this:

class Foo {

  namespace {
    read_write: [Method_1()];    
    read_only:  [Method_2()];
   
    char data1;
    float data2;
  };
 
public:
  void Method_1();
  void Method_2();
  void Method_3();
 
private: // global to class
  int x;
  int y;
};
 
 
 
 
-- 
 Std-Proposals mailing list
 Std-Proposals@lists.isocpp.org
 https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals