Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:54:45+00:00 2026-06-01T07:54:45+00:00

I have a data structure Person struct Person { protected: string name; int age;

  • 0

I have a data structure “Person”

struct Person
{
  protected:
    string name;
    int age;
    string address;
    ...
}

I want to create “views” around this structure to separate out access to different member variables:

class PersonRead: public Person
{
  public:
    string getName() {..}
    int getAge() {...}
    ...
}
class PersonUpdate: public Person
{
  public:
    void setAddress( string address_ ) {...}
    void setAge( int age_ ) {...}
    ...
}

I use this to only expose those methods/variables which are really required:

int main()
{
...
    writePersonDataToFile ( (PersonRead) personObj );
    updatePersonData ( (PersonUpdate) personObj);
...
}

Though the above code serves my purpose, there are several issues including:

  1. The public inheritence here is not exactly an ‘is-a’ relationship

  2. I need to derive IndianPerson from Person, and all the corresponding interfaces. This leads to bad diamond pattern:

    struct IndianPerson: public Person {};
    class IndianPersonRead: public IndianPerson, public PersonRead {}; //Person Class common, Diamond pattern here!
    

Is there a name for such a design pattern? What are better ways to implement this pattern? I have a feeling Policy classes might help, but cant figure out how to implement this

Any examples would be great help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:54:46+00:00Added an answer on June 1, 2026 at 7:54 am

    For your scenario this might seem like overkill but, if you want fine grained control over which classes can call different methods on your class the c++ client-attorney idiom idiom might be appropriate.

    For a detailed description of this idiom see http://drdobbs.com/184402053

    Here is a rough example (note: this has not been compiled, although it is based on production code I am currently using):

    class Person
    {
    public:
       /// constructor destructor etc:
    
    private:
        string getName() { return name; }
    
    public:
        /// Writer Attourney that access to allows class PersonReader access 
        /// to getXXX functions
        class ReaderAttorney
        {
        private:
            /// Add additional reader member functions...
            static string readName( Person& p )
            { 
                return p.getName();
            }
    
            // Make any classes that shuold be allowde read access friends of the 
            // attorney here
            friend class PersonReader;
        };
    
        /// Writer Attourney that access to allows class PersonWriter access 
        /// to setXXX functions
        class WriterAttorney
        {
        private:
            /// Add additiona reader member functions...
            static string setName( Person& p, const string& newName )
            { 
                p.setName( newName );
            }
            friend class PersonWriter;
        };
    
    private:
        string name;
        int age;
        string address;
    };
    

    This can be used as follows:

    void PersonWriter::setPersonDetails( const string& name, int age .... )
    {
       // PersonWriter is a frend of WriterAttorney and is granted access
       Person::WriterAttorney::setName( name );
       Person::WriterAttorney::setName( age );
    
       // Note this will fail, since PersonWriter is not a friend of 
       // ReaderAttorney, ergo it is not granted read permission:
       Person::ReaderAttorney::readName();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data structure: public class Person { public string Name{get;set;} public
I have a Person class with two properties: name and address . I want
I have a data structure that represents C# code like this: class Namespace: string
I have a data structure defined as struct myDataStruct { int32_t header; int16_t data[8];
I have a data structure where an entity has times stored as an int
I have an application that deals with data in the following structure: struct Message
Let's say I have the following hypothetical data structure: create table country ( country_id
I have a data structure which uses composite ids (Which I dont wish to
I have a data structure which is as given below: class File { public
Guys, I have a data structure which has 25 distinct keys (integer) and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.