S
S S
Hi
I have a requirement where I am declaring a map within a class.
class abc {
map <void*, void*> mMap; // I do not pass compare struct here.
....
};
Here I am not passing compare function, but I want to do it into the
constructor of class abc. Is it possible? Above void* could well be
char* and in that case user will initialize it with some char compare
function , but I don't know how to accomplish above task. Can I do
something like
abc::abc() : mMap(compare_string) {}
But above dosen't work !!
So I want to try something like function pointers, see the code below
bool comp_default(const void *a, const void *b) { return a < b; }
bool CharStringCompare(const void* a, const void* b) {return
strcmp((char*)a, (char*)b) < 0;}
struct compare_key_ {
bool operator()(const void* a, const void* b)
{
return (abc::*mComp)(a, b); // I know this is wrong as I am not
using object here
}
};
class abc {
friend struct compare_key_;
bool (abc::*mComp) (const void*, const void*) ;
map<void*, void*, compare_key_> mMap;
....
};
But above code is not the correct thing I am doing, I just wanted to
let you know the direction I am thinking? Can someone please help me
out.
Thanks
S
I have a requirement where I am declaring a map within a class.
class abc {
map <void*, void*> mMap; // I do not pass compare struct here.
....
};
Here I am not passing compare function, but I want to do it into the
constructor of class abc. Is it possible? Above void* could well be
char* and in that case user will initialize it with some char compare
function , but I don't know how to accomplish above task. Can I do
something like
abc::abc() : mMap(compare_string) {}
But above dosen't work !!
So I want to try something like function pointers, see the code below
bool comp_default(const void *a, const void *b) { return a < b; }
bool CharStringCompare(const void* a, const void* b) {return
strcmp((char*)a, (char*)b) < 0;}
struct compare_key_ {
bool operator()(const void* a, const void* b)
{
return (abc::*mComp)(a, b); // I know this is wrong as I am not
using object here
}
};
class abc {
friend struct compare_key_;
bool (abc::*mComp) (const void*, const void*) ;
map<void*, void*, compare_key_> mMap;
....
};
But above code is not the correct thing I am doing, I just wanted to
let you know the direction I am thinking? Can someone please help me
out.
Thanks
S