What’s the point of a const member function? If you asked me last week, I would have said that const in general is just a “safety net” to protect you from yourself. I would have proceeded to babble something about how mutators must be non-const, and accessors should always be const.
I’m sure I would have said “always”.
But last night I realized that, in certain instances, you can optimize by breaking this rule.
Continue reading ‘Optimizing with Const Member Functions’
I’ve had a minor revelation since my last two posts about C++. I don’t actually need the new auto keyword. Check it out:
#include
#include
#include
using namespace std;
using namespace boost;
void hello( int a )
{
while( a– ) cout << "Hi Mom: " << a << endl;
}
int main()
{
const int count = 5;
function< void (void) > func = bind( hello, count );
func();
}
I’m still not exactly sure how this works, but I’m positve that boost::function rocks.
I’ve been thinking about my C++ problem.
I initially expected the return type of the bind expression would be void (*)(void). Obviously (well, it’s obvious now, anyway), this is not what bind returns. Rather, bind returns a function object (aka, a functor). A functor is just an object that implements operator(), making itself a “callable entity”. In my case, bind is returning a functor which implements void operator()(void) — which is similar, but not quite the same as a “nullary” function pointer void (*)(void).
So, what’s the type of the functor? Turns out it’s:
class boost::_bi::bind_t > >
Couldn’t be simpler than that, eh? How do I know this? The compiler tells me the type when it errors-out during compilation. And that is the most frustrating thing of all. The compiler knows the damn type, but there is no way for me to tell it, “Yes, yes! Just use one of those, thanks.”
At least, not until we get Dave Abraham’s new auto keyword.
I love C++, really I do, but damn my head hurts.
#include
#include
using namespace std;
using namespace boost;
void hello( int a )
{
while( a– ) cout << "Hi Mom: " << a << endl;
}
int main()
{
const int count = 5;
/*some type*/ func = bind( hello, count );
func();
}
The problem is, what the hell should go in place of /*some type*/? I expected void (*)(void), but that doesn’t work.
Latest Comments
RSS