Even after years C++ is confusing the hell out of me..
My class implements
virtual CStatCounter& operator= (CStatCounter const& inSC);
and
virtual CStatCounter operator+(const CStatCounter& rhs);
And I’m keeping a
vector<CStatCounter*> mStatistics
somewhere else. Now all I’m trying to do is accumulate all values.
After being laughed at by std::accumulate I’ve switched to a simple loop rolled on my own, still no luck:
CStatCounter *iniCounter = new CStatCounter(0);
BOOST_FOREACH (CStatCounter *counter, mStatistics)
{
iniCounter = iniCounter+counter;
}
The compiler (Xcode 4.2/clang) complains about
Invalid operands to binary expression ('CStatCounter *' and 'CStatCounter *')
I can change the loop body to counter+counter and it still fails with the same error message.
Shouldn’t it be smart enough to implicitly convert between references and pointers?
Am I missing something trivial here?
It could, but that wouldn’t be C++, would it? When you can overload operators for every conceivable combination of operands, it’s really not safe to make such assumptions.
Dereferencing:
By the way, you can still use std::accumulate, you just need a custom binary operation, like this: