[コンテキスト:Java、4ヶ月新作; C++の古い手。]Javaの固定サイズ配列の代わりに?
私は多くの場所で固定サイズの配列( "固定ストリング")を必要とするライブラリに取り組んでいます。私は、この特定の問題のための依存性注入(q.v.)を使用しようとしているので、私は、フォームの何か希望:
class Foo
{
private Bar injectedBar;
private char[] injectedFixedString;
Foo(Bar injectedBar, /* what can go here? */ char[5] injectedFixedString);
{ /* initializing code goes here /* }
}
シンプル必要ですが - これは、自動生成された通信プロトコルに起こっているの。私はそれが由来しているプロトコルとデータベースの制御をゼロにしています。最終的なコードには、これらのインスタンスが数千ではないにせよ数百もあるでしょう。カスタムクラスを作成するために
char injectedFixedString[5];
:
はC++への私の唯一の選択肢です:だから、すべてのことを与えられましたか?次のようなもの:
class FixedBarString {
/* could also set this in the constructor, but this complicates code generation a tad */
public static integer STRING_SIZE = 5; /* string size */
char[] fixedString = new char[STRING_SIZE];
FixedBarString(char[] string) throws RuntimeException {
/* check the string here; throw an exception if it's the wrong size.
I don't like constructors that throw however. */
}
public void setString(char[] string) throws RuntimeException {
/* check the string here */
}
public char[] getString() {
/* this isn't actually safe, aka immutable, without returning clone */
}
public char[] createBlankString() {
return new char[STRING_SIZE];
}
}
ありがとうございます。 (コードがあまりにも多すぎると私の謝罪です)。
は答えを書いた後、私は実際にあなたがここで何をしようとして従っていません実現しました。何がFoo(Bar injectedBar、/ *ここに行くことができますか?*/char [5] injectedFixedString); 6文字の文字列を渡したらどうしますか?私がC++がどのように処理したかを知っていれば、おそらくあなたを助けることができます。 – Endophage
OPはC++の固定サイズの配列の代替品を探しています。C++では以下のメソッド宣言が有効です: 'myMethod(char myarray [5])' – dogbane
@dogbane:そしてその宣言は何ですか? –