where major = need to track a large number of them in a sorted vector; they are heavily referenced outside the vector
Having a coding revelation!!!
OBSERVATION ONE
QObjects cannot normally be copied
this is by design - their copy constructors and assignment operators are private
why? A Qt Object...
might have a unique QObject::objectName(). If we copy a Qt Object, what name should we give the copy?
has a location in an object hierarchy. If we copy a Qt Object, where should the copy be located?
can be connected to other Qt Objects to emit signals to them or to receive signals emitted by them. If we copy a Qt Object, how should we transfer these connections to the copy?
can have new properties added to it at runtime that are not declared in the C++ class. If we copy a Qt Object, should the copy include the properties that were added to the original?
in other words, a QObject is a pretty serious object that has the ability to be tied to other objects and resources in ways that make copying dangerous
isn't this true of all serious objects? pretty much
OBSERVATION TWO
if you have a vector of objects, you often want to track them individually
if you use a vector of pointers, you can keep the pointer and not worry about vector reallocations - perfect
OBSERVATION THREE
STL provides everything you need to keep vectors of your pointers, iterate thru them, etc.
for a vector of any substantial size, you want to keep objects sorted so you can find them quickly
that's what my sorted_vector class is for
following STL practices, to get sorting, you have to provide operator< for whatever is in your vector
BUT... you are not allowed to do operator<(const MyObjectPtr* right) because it would require a reference to a pointer which is not allowed
BUT... you can provide a FUNCTOR to do the job, then provide it when sorting/searching
a functor is basically a structure with a bool operator()(const MyObjectPtr* left, const MyObjectPtr* right)
OBSERVATION FOUR
C++11 provides lambda functions, we should switch from iterators and functors to those
but not quite yet, since we're writing cross-platform code
start with trying to use this cool for_each loop with a lambda instead of the traditional for-with-increment loop:
vector<int> v;
for_each( v.begin(), v.end(), [] (int val)
{
cout << val;
} );
do not touch this tho until we can be sure that all platforms provide compatible C++11 handling
max os x snow leopard does not understand the lambda function
|