[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

C++ problem



Hej!

Jeg forsøger at lave en template klasse til at tage averages dvs.

template<class T>
class CAverage
{
  private:
    unsigned int n;
    T x;
    
  public:
    CAverage(void): n(0) {x=0;};  // PROBLEM!!

    void Add(const T& y)
      {
        x+=y; n++;
        if (n == 0xffffffff) {cerr << "Overflow in Average!!\n";}
      };
    
    T Average() const
      {
        if (n==0) return x;
             else return x/n;
      }
};

Jeg ønsker at ovenstående klasse skal kunne tage averages af
hvad som helst, hvor operatore T operator +=(const T&),
T operator/=(double), og copy operationen er definerede.

Problemet er nul elementet. Hvordan specificere jeg et nul
element uden at kende typen på forhånd?

Rent praktisk er T en hjemme implementet dynamisk vector
klasse af 3 doubles: CDynVector<double,3>.

Kan jeg implementere et nul element, således at ovenstående
klasse automatisk finder ud af hvad det relevante nul element
er for class T??

Jeg spekulerede på noget som T::Zero hvor Zero er et statisk
element af typen T i alle T klasser. Men jeg vil meget nødigt
øge hukommelses forbruget, fordi det er kritisk. (P.t. bruger
jeg 250Mb ram når jeg køre programmet).

Jeg tror p.t. jeg bliver nød til at lave en metode i CDynVector,
der initalisere et element til 0.

Så "x=0;" bliver x.zero();

Det syntes dog lidt upraktisk ifht. en notation som x=(T)0;
eller x=T::Zero;

Nogle forslag??

-- 
  Carsten Svaneborg   http://www.mpip-mainz.mpg.de/~svanebor
<out of signature error, please reboot internet>


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *