f



user-defined predicate function inside class

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! ]
0
martinasi (1)
6/26/2003 10:28:30 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

1 Replies
813 Views

Similar Articles

[PageSpeed] 0

Hello Pavel!

Pavel schrieb:

 > 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.


 >
 > /* 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;};

 >
 >    char* getAddress(){return m_szAddress;};

// Provide const-overload for this:
const char* getAddress() const {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);
      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! ]
0
Daniel
6/27/2003 6:02:12 PM
Reply: