Re: Function object, this and constructor
On Jul 9, 1:11 am, vulpes <ntv...@luukku.com> wrote:
This similar situation will explain somewhat, what I want to do:
class X {
public:
X() : map_(*this) {}
bool operator() (int a, int b) { return goodStuff(a) <
goodStuff(b); }
int goodStuff(int a) { ... }
private:
map<int, int, X> map_;
};
Why do I want this? Because the goodStuff function is already there
and I don't want to write it twice. This piece of code doesn't work as
one would expect.
What would one expect? It's undefined behavior. (I expect that
it will not compile with most implementations. I can't think of
any reasonable implementation of map where the class itself
doesn't contain an instance of the comparator. So what you've
written is that X contains a map which contains an X...)
I also tried doing things like
class X {
public:
X() : map_( ptr_fun(&X::stuff) ) {}
bool stuff(int a, int b) const { return goodStuff(a) <
goodStuff(b); }
private:
map< int, int, binary_function<int, int, bool> > map_;
};
but this works neither. Is there a way to do this thing.
The obvious (but not necessarily good) solution is to replace
the map with a pointer to the map. Alternatively, just about
any form of indirection in the comparator should work:
class X ;
class XCmp
{
public:
XCmp( X const* owner ) ;
bool operator()( int, int ) const ;
private:
X const* myOwner ;
} ;
class X
{
// ...
private:
std::map< int, int, XCmp >
myMap ;
} ;
The X objects
are a pain to construct (lots of big stuff in 'em) so I don't want to
generate them anymore than I have to.
Then you almost certainly don't want to use one directly as
comparator. The actual map uses a *copy* of the object it is
passed in the constructor (or a default constructed instance, if
it isn't passed a comparator).
I guess that second could be
made to work if the goodStuff would be static, but it ain't.
You could probably work out something using boost::bind. The
basic problem is that you have a function with three arguments
(the X and the two ints), and map will want to call it with two,
so you need to use a functional object which contains a pointer
to (or a reference to) the X. And you can't do it with the
standard binders, because the (current) version of the standard
doesn't support functional objects with more than two
parameters.
This isn't a stopper problem, I know a way around this
(problem- specific), but it stinks. I want to know how to do
this "the way it should be done".
"The way it should be done" probably depends on the context, but
most likely involves a comparator object with a pointer to (or a
reference to) the owning X.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34