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

  • SEARCH
  • Home
  • 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 138935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:20:43+00:00 2026-05-11T07:20:43+00:00

Continuing from the question that I asked here: C++ multi-dimensional data handling In my

  • 0

Continuing from the question that I asked here: C++ multi-dimensional data handling

In my example: I have many Chips, each Chip has many Registers, each Register has many Cells, and each Cell has many Transistors. I asked whether to use one complex STL container for them, or to implement full classes for them. And, as advised, I chose to implement full classes for them. I have:

class Chip {     map<RegisterLocation, Register> RegistersPerLocation; };  class Register {     map<CellLocation, Cell> CellsPerLocation; };  // etc.. 

Now, I need to fill the data to the classes, and I can’t decide: Should reading the data be responsibility of these classes, or should they just wrap the containers and the reading will be done outside.

I mean I have to choose one of the following: Either:

class Chip {     map<RegisterLocation, Register> RegistersPerLocation;   public:     void AddRegisterPerLocation(RegisterLocation, Register);  }; void ReadChipData(Chip & chip) {     for (RegisterLocation loc = 0; loc < 10; loc++)     {         Register reg;         ReadReg(reg);         chip.AddRegisterPerLocation(loc, reg);        } }      void ReadReg(Register & reg) {     for (CellLocation loc = 0; loc < 10; loc++)     {         Cell cell;         ReadCell(cell);         reg.AddRegisterPerLocation(loc, cell);        } } //etc... 

Or:

class Chip {     map<RegisterLocation, Register> RegistersPerLocation;   public:     void ReadData();  }; void Chip::ReadData()  {     for (RegisterLocation loc = 0; loc < 10; loc++)     {                   Register reg;         reg.ReadData();         RegistersPerLocation[loc] = reg;        } } //etc... void ReadChipData(Chip & chip) {     chip.ReadData(); }      

Thank you.

  • 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. 2026-05-11T07:20:43+00:00Added an answer on May 11, 2026 at 7:20 am

    If you are thinking of tying the reader/writer to the domain objects in order to follow the principle of encapsulation, you are correct to a certain extent. But remember: You bind not just any action, but a valid behavior. Valid as in makes sense for the object in the domain.

    Another thing to keep in mind is separation of concerns. Serializability is not a Chip‘s intrinsic behavior — modeling that into the domain object would be unfair IMO. YMMV.

    Separate the reading(and writing) from the classes. As the library does. Expose iterators if you have to. And you can overload the ‘<<‘ and ‘>>’ operators for syntactic sugar 😉

    A minor nit on the classes — a template based approach looks so promising.

    Here’s some code I cooked up: you can try the following as well. (I’ve successfully compiled and run this on a MS VS2005 but check it out on your system. Also, can someone fix the tabbing — feeling too lazy to do this :P)

    /*+-----------8<----------------------------8<-----------+*/ #include <vector> #include <iostream> #include <algorithm> #include <map> #include <iterator>  /* mother template */ template<class _Item> struct Hardware {            Hardware() : _myCont(2 + ::rand() % 5) {} private:     typename vector<_Item> _myCont;      // i/o friends     template<class _Item>      friend ostream& operator<<(ostream& output,                                 const Hardware<_Item>& me);     template<class _Item>     friend istream& operator>>(istream& in,                                 const Hardware<_Item>& me);  };  /* actual domain objects */ /* base object */ struct Transistor { }; /* built objects */ typedef Hardware<Transistor> Cell; typedef Hardware<Cell> Register; typedef Hardware<Register> Chip;  /* poorman's introspection utility */ template<class T> const char *who() { return ''; }  template<> const char *who<Transistor>() { return 'Transistor'; }  template<> const char *who<Cell>() { return 'Cell'; }  template<> const char *who<Register>() { return 'Register'; }  template<> const char *who<Chip>() { return 'Chip'; }  /* writer/serialize out */ template<class T> ostream& operator<<(ostream& out, const Hardware<T>& hw) {     // whatever you need to do to write     // os << chip works fine, because you will provide a specialization     out << '[ ' << ::who<Hardware<T>>() << ' ]\n\t';      std::copy(hw._myCont.begin(), hw._myCont.end(),          std::ostream_iterator< T >(std::cout, '\n\t'));      return out; }  /* specialize for base object */ ostream& operator<< (ostream& out, const Transistor& hw) {     out << '[ ' << ::who<Transistor>() << ' ]\n';     return out; }   /* reader/serialize in */ template<class T> istream& operator>>(istream& in, const Hardware<T>& hw) {     // whatever you need to do to read     // similarly in >> chip works fine,      return in; }  // driver showing relationships // Chip -> Register -> Cell -> Transistor int main() {     Transistor t;     std::cout << t << std::endl;     Cell ce;     std::cout << ce << std::endl;     Register r;     std::cout << r << std::endl;     Chip C;     std::cout << C << std::endl; } /*+-----------8<----------------------------8<-----------+*/ 

    Caveat: Haven’t tested, so there may be quite a few compiler errors/warnings. But this should give you an idea of what I am trying to say.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 191k
  • Answers 191k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Rather than hard code the path to your Lua script… May 12, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer Among other things, it leads to clarity of representation -… May 12, 2026 at 6:14 pm
  • Editorial Team
    Editorial Team added an answer Put a web.config file at the root of the asp… May 12, 2026 at 6:14 pm

Related Questions

Not sure if this is the usual sort of question that gets asked around
I've recently come into a web development position with a company who just lost
I asked another question: https://stackoverflow.com/questions/1180240/best-way-to-sort-1m-records-in-python where I was trying to determine the best approach
I'm new to sed, and need to grab just the filename from the output

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.