I then have another class called materialList that takes the objects and adds it to the list |
Avoid designing classes with materially (get it?) the same member functions as a piece of member data. For example, consider
materialList. This is a list class whose sole purpose is to serve as a proxy to a
std::list.
It is easier and more direct to use a std::list.
If only one of a particular thing is ever created, don't design a class to represent it. One will have ample opportunity to do this extra work
later when and
if it's a good idea. For example, there is only one
materialList in the entire program - so it may as well not exist at all.
If a class's get/set functions do nothing, the accessed data is
effectively public. Just make it public.
Trivial getters and setters hinder comprehension. One can't say much of anything about the meaning of
getThing(), and even less of the corresponding setter. But one can say everything there is to know about
foo.thing = bar without more than a glance at
foo. For example,
item is about 40 lines long but it should be five:
1 2 3 4 5
|
struct item
{
std::string name;
int mesh, material
};
|
This is an 90% reduction in code size & a 0% reduction in functionality. Each line of code represents a chance to get confused and a chance to make an error; less is more.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <iterator>
struct item
{
std::string name;
int mesh, material;
};
std::ostream& operator<<(std::ostream& s, item const& i)
{ return s << i.material << ' ' <<i.mesh << ' ' << i.name << '\n'; }
std::istream& operator>>(std::istream& s, item& i)
{ return s >> i.material >> i.mesh >> i.name; }
bool operator<(item const& a, item const& b)
{ return a.material < b.material; }
int main()
{
std::multiset<item> items(std::istream_iterator<item>{std::cin}, {});
std::copy(items.begin(), items.end(), std::ostream_iterator<item>{std::cout});
}
|
http://coliru.stacked-crooked.com/a/f6c66a7632032943