c++匿名類—指針

1 摘自網上

2代碼實例

#include <iostream>
#include <list>
#include <iterator>
#include <cmath>

using namespace std;

class Term
{
    public:
        Term(int c,int e):coef(c),exp(e){

        }
        float TermValue();
        int GetCoef()
        {
            return coef;
        }
        int GetExp()
        {
            return exp;
        }
        static void SetX(float x_t)
        {
            x=x_t;
        }
        int GetX()
        {
            return x;
        }
    private:
        int coef;
        int exp;
        static float x;
};

float Term::x=1.0;

float Term::TermValue()
{
    return coef*pow(x,exp);
}

int main()
{
    list<Term> poly;
    list<Term>::iterator begin,end;
    int i;
    float  result=0;
//此處使用了c++中匿名內部類,區分java,c++中new後是一個指針類型
    for(i=1;i<5;i++) poly.push_back(Term(i,i));
    begin=poly.begin();
    end=poly.end();
    begin->SetX(2);
    do
    {
        result+=begin->TermValue();
        begin++;
    }while(begin!=end);
    cout<<result<<endl;
}

 

輸出結果:98 正確java

c++中的匿名內部類不須要new,new後是一個指針類型;java中的匿名內部類是一個引用類型。 分清楚。ios

相關文章
相關標籤/搜索