2017-05-31 6 views
1

おはよう! Javaにはこのようなコードがあり、C++とQTで同じことをする必要があります。Callableインターフェイスを置き換えるにはどうすればいいですか、別のスレッドや別のものを使用する必要があるのは分かりますか?C++とQtの呼び出し可能なインターフェイスアナログ

public class Filter implements Callable<short[]> { 

protected short countOfCoefs; 
protected double[] coefsFilter; 
protected short[] inputSignal; 
protected short[] outputSignal; 
protected double gain; 

public Filter(final int lenghtOfInputSignal){ 
    gain = 1; 
    this.outputSignal = new short[lenghtOfInputSignal]; 
} 


public void settings(final double[] coefsFilter, final short countOfCoefs, final short[] inputSignal) { 
    this.inputSignal = inputSignal; 
    this.coefsFilter = coefsFilter; 
    this.countOfCoefs = countOfCoefs; 
    this.outputSignal = new short[inputSignal.length]; 
} 

private convolution[] svertka() { 
    int multiplication; 
    for(int i = 0; i < inputSignal.length - FilterInfo.COUNT_OF_COEFS; i++) { 
     for(int j = 0; j < this.countOfCoefs; j++) { 
      multiplication = (int) (this.inputSignal[i] * this.coefsFilter[j]); 
          this.outputSignal[i+j] += gain * (short)(multiplication); 
     } 
    } 
    return this.outputSignal; 
} 

public void setGain(float d) { 
    this.gain = d; 
} 

public double getGain() { 
    return this.gain; 
} 

public short[] getOutputSignal() { 
    return this.outputSignal; 
} 

public long getCountOfSamples() { 
    return this.inputSignal.length; 
} 

@Override 
public short[] call() throws Exception { 
    this.svertka(); 
    return this.outputSignal; 
} 

}

+1

デストラクタのみが定義されたshort []またはstd :: vector を受け入れる抽象クラスCallableを定義できます。スレッドの必要はありません。 –

+1

抽象クラスの必要もありません。 C++はすべてのものを必要としません。 C++でJavaをコード化しない方がいいです。その強みにはそれぞれの言語を使います。 – juanchopanza

+0

@juanchopanza - 抽象クラスでは、少なくとも1つの純粋仮想メソッドを含むクラスを参照しています。 C++のこの機能は、Javaの最初の一般公開より先行しています。 Stroustrupは、1994年の著書「C++の設計と進化」でそれをカバーしています。 Java 1.0は1995年にリリースされました。抽象クラスの概念は言語固有ではありません。メソッドCallable.call()は仮想でなければなりません。私が知る限り、それを純粋な仮想にすることは余計な費用をかけることなく、誤って非サブクラスのCallableを渡す可能性を排除します。この目的のために抽象クラスをC++で使用するのが適切です。 –

答えて

0

があり、複数のアプローチであり、正しいものを選択すると、あなたが達成したいのかによって異なります。あなたが複数のスレッドをしたい場合は

1)、あなたはについて読むことができます:

1a)スレッド化のQtサポートは、thisのマニュアルページ

1b)std::packaged_taskC++ thread library注:あなただけに振る舞うオブジェクトが必要な場合)

class ICallable 
{ 
    public: 
     virtual ~ICallable() {} 
     virtual void Call(std::vector<short> shorts) = 0; 
}; 

3:これは、あなただけのインターフェイスが必要な場合は、次のようにC++でそれを実装することができます)C++ 11コンパイラ

2が必要です関数のように、あなたはファンクタを作成したり、ラムダ(場合には、あなたがC++ 11のサポートを持っている)を使用することができます:STDをカスタマイズする

class myFunctorClass 
{ 
    public: 
     myFunctorClass() {} 
     int operator() (int x) { return x * x; } 
    private: 
     //you can have member variables, initialize them in constructor 
}; 

ラムダ例を::ソート

std::vector<int> v; 
//populate v 
std::sort(v.begin(), v.end(), [](int l, int r) { return l > r; }); 
関連する問題