inheritance overkill?

Started by
9 comments, last by GenuineXP 15 years, 3 months ago
Quote:Original post by twoaterisn
By external objects reading the data, do you mean that i should move the checkcorrectness() outside the atom class? wouldn't this be C-style programming or is it just that i don't get what you're saying? :)

Possibly, yes, much like the function foo in my template example accesses the data of bar. In this way, atoms just contain data. Again, depending on exactly what you wish to model, that data could easily be static. If this is the case, it makes more sense for operations to be external (of atoms).

As mentioned above, using a table may be the simplest and best suited approach (forget my template ramblings). You could probably implement something like the following.

struct atom {    // Name, valance, mass, etc. Just data.};typedef std::vector<atom> atom_con_t;typedef atom_con_t::iterator atom_iter_t;typedef atom_con_t::const_iterator const_atom_iter_t;class molecule {public:    molecule(const atom_con_t& table); // References atoms in this table.private:    typedef std::vector<const_atom_iter_t> atom_ref_con_t;    atom_ref_con_t atoms_;};void create_atomic_table(atomic_table_con_t& table, std::istream& in);int main() {    atomic_table_con_t table;    std::ifstream fin("table.dat");    create_atomic_table(table, fin);    // Use the table.}


Something like this may suffice; it really depends on what you're shooting for. In this example, atoms are just structs that contain atom data (such as valence and atomic mass). A table of atoms (elements) is just a vector of atoms, which could be read from some input stream (like a file). Once this table is created, it can be passed to molecules what store references into the table. I've used iterators here. The typedefs just help keep things clean and easier to remember.

What exactly do you plan to model? Knowing this would probably make it easier for others to help you. For example, if you need to model the actual bonds between atoms within a molecule, what I've done here may be too simplistic.

EDIT: You could also use a map in this approach, where the key is whatever you would most frequently use to identify a particular atom (probably name or valence).

[Edited by - GenuineXP on January 16, 2009 11:28:35 AM]

This topic is closed to new replies.

Advertisement