C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

user-defined predicate function inside class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Pavel
Guest





PostPosted: Thu Jun 26, 2003 10:28 pm    Post subject: user-defined predicate function inside class Reply with quote



I have problem to desing the user-defined predicate function
arch_Compare for Std C++ sort algorithm inside the class <Parch>.
The only way I found was declare this function as friend, but then
m_nSortKey have to be static.
/*friend bool arch_Compare( PArchElem elem1, PArchElem elem2 );
static int m_nSortKey;*/
Is there another solution where m_nSortKey donīt have to be static?
Thanks.

/* source code which is not correct for Visual C++*/
class PArchElem{
private:
char szName[41];
char szAddress[41];
public:
PArchElem(){};
~PZakArchiv(){};
char* getName(){return m_szName;};
char* getAddress(){return m_szAddress;};
};

class PArch{
private:
deque<PArchElem> m_dqArch;
int m_nSortKey;
public:
PArch();
~PArch();
bool PArch::arch_sort();
bool arch_Compare (PArchElem elem1, PArchElem elem2);
};
bool PArch::arch_sort(){
sort(m_dqArch.begin(),m_dqZakArch.end(),arch_Compare);
return true;
}
bool PArch::arch_Compare( PArchElem elem1, PArchElem elem2 ){
double dResult;
switch(m_nSortKey){
case NAME:
dResult=lstrcmpi(elem1.getName(),elem2.getName());
break;
case ADDRESS:
dResult=lstrcmpi(elem1.getAddress(),elem2.getAddress());
break;
default:
dResult=0;
break;
}
return dResult>0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel Spangenberg
Guest





PostPosted: Fri Jun 27, 2003 6:02 pm    Post subject: Re: user-defined predicate function inside class Reply with quote



Hello Pavel!

Pavel schrieb:

Quote:
I have problem to desing the user-defined predicate function
arch_Compare for Std C++ sort algorithm inside the class <Parch>.
The only way I found was declare this function as friend, but then
m_nSortKey have to be static.
/*friend bool arch_Compare( PArchElem elem1, PArchElem elem2 );
static int m_nSortKey;*/
Is there another solution where m_nSortKey donīt have to be static?
Thanks.


In general, a much more flexible and encapsulated ansatz is the usage of

functors instead of functions. This has the additional advantage, that
the
comparator could simply contain the sortkey itself.
Additionally I would recommend, that the arguments of your comparator
should better be const PArchElem& instead of PArchElem.


Quote:

/* source code which is not correct for Visual C++*/
class PArchElem{
private:
char szName[41];
char szAddress[41];
public:
PArchElem(){};
~PZakArchiv(){};
char* getName(){return m_szName;};

// Provide const-overload for this:
const char* getName() const {return m_szName;};

Quote:

char* getAddress(){return m_szAddress;};

// Provide const-overload for this:
const char* getAddress() const {return m_szAddress;};


Quote:

};


class PArch{
private:
deque<PArchElem> m_dqArch;
int m_nSortKey;
public:
PArch();
~PArch();
bool PArch::arch_sort();
// bool arch_Compare (PArchElem elem1, PArchElem elem2);
struct ArchCompare;
};

struct PArch::ArchCompare
{
explicit ArchCompare(int sortKey) : sortKey_(sortKey){}

bool operator()(const PArchElem& elem1, const PArchElem& elem2) const
{
double dResult; // Why double and not int???
switch(sortKey_){
case NAME:
dResult=lstrcmpi(elem1.getName(),elem2.getName());
break;
case ADDRESS:
dResult=lstrcmpi(elem1.getAddress(),elem2.getAddress());
break;
default:
dResult=0;
break;
}
return dResult>0;
}
private:
int sortKey_;
};

bool PArch::arch_sort(){
sort(m_dqArch.begin(),m_dqZakArch.end(),ArchCompare(m_nSortKey));
return true;
}

Hope that helps,

Daniel Spangenberg




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.