1

2 つの整数を引数として取るコンストラクターを作成する必要があります。

そこから、参照によってこれらの整数を取得するメソッドを呼び出す必要があります。このメソッド内で、整数をchar*型 (数字の配列) に動的に変換する必要があります。

コンストラクターの最後には、char*初期整数の代わりに 2 つの配列が必要です。

他のクラスが同じことを構造上で行うため、私はこのようにする必要があります。それらをテンプレート属性に保存します。


私は c++ 言語は初めてですが、最初に推測したのはテンプレートを使用することでした。このトピックについて少し調査したところ、うまくいくはずであることがわかりました。

自分ですべてをコンパイルしたいのですが、頭の中で C++ クラスを実装するのが面倒で、かなり長いコンパイル エラーのリストが生成されます。


最初の質問 - これはテンプレートを使用して実行できますか? 2 番目の質問です。私はすでに自分で何かを書いているためです。

template <class type> class Addition {
   type num_a;
   type num_b;
   void convert(type, type);
public:
   Addition(type, type);
}

template <class type> Addition::Addition(type a, type b) {
   convert(&a, &b);
   num_a = a;
   num_b = b;
}
template <class type> Addition::convert(type *a, type *b) {
   int temp_a = a, temp_b = b;
   a = char[256], b = char[256];
   // converting
}

これは大丈夫ですか、それとも何か間違ったことをしましたか?
C++ でクラスを実装する方法について何か提案はありますか?

次のような値で属性を初期化できないのはなぜですか。

template <class type> class Addition {
   type outcome = 0;
}

また、C++ でこのキーワードを使用する必要がない場合、次のようにするにはどうすればよいですか?:

template <class type> Addition::Foo(type a, type b) {
   this->a = a; // a = a;
   this->b = b; // b = b;
}
4

2 に答える 2

2

免責事項:あなたがやっていることのために本当にテンプレートが必要かどうかを判断することはできません. これは、Adddition クラス テンプレートで使用するさまざまな型の数によって異なります。のみに使用する場合はint、不要な複雑さが生じる可能性があります。後でいつでもリファクタリングできます (これがアジャイル アプローチです)。

そうは言っても、テンプレートを使用する場合、通常の規則はTテンプレート パラメーターを記述し、クラス テンプレート内にtypeネストされたを使用することです。orのtypedef使用は好みの問題ですが、組み込み型も引数として渡すことができるという事実を強調します。ただし、template-template パラメーターを使用すると、次のように記述する必要があることに注意してください。typenameclasstypename

template<template<typename> class U> SomeClass { /* your definition */ };
                            ^^^^^ // <-- NOT typename here 

これは、引数として渡すことができるのはクラス テンプレートのみであるという事実を強調しています。

コンパイルに失敗する可能性のあるコードについて言及できる他のいくつかの小技があります (戻り値の型がconvert()欠落しており、クラス定義にセミコロンが欠落しています)。

template <typename T> 
class Addition 
{
   static const std::size_t N = 256; // are you sure that 256 is all you'll ever need?
   T num_a;
   T num_b;
   void convert(T const*, T const*); // by T const*, not T*
public:
   Addition(T const&, T const&); // by T const&, not T
}; // <-- make sure to end class definitions with a semi-colon!

template <typename T> 
Addition::Addition(T const& a, T const& b) 
{
   convert(&a, &b);
   num_a = a;
   num_b = b;
}

template <typename T>
void Addition::convert(T const* a, T const* b) // <-- use T const* if you don't modify the parameters
^^^^ // <-- you forgot the return type
{
   int temp_a = a, temp_b = b;
   a = char[N], b = char[N]; <-- hardcoded 256 is bad practice, better to keep that in 1 place only
   // converting
}

C++11 では、委譲コンストラクター(最新の Visual C++ ともちろん gcc/Clang でサポートされています) を使用して、

template <typename T> 
Addition::Addition(T const& a, T const& b) 
:
    Addition(&a, &b) // delegate to the other constructor
{}

template <typename T>
Addition::Addition(T const* a, T const* b) // <-- use T const* if you don't modify the parameters
{
   int temp_a = a, temp_b = b;
   a = char[N], b = char[N]; <-- hardcoded 256 is bad practice, better to keep that in 1 place only
   // converting
}

最後に、テンプレート定義はとにかくヘッダーにある必要があるため、次のようにクラス定義内にすべてを記述することもできます。

template <typename T> 
class Addition 
{
   static const std::size_t N = 256; // are you sure that 256 is all you'll ever need?
   T num_a;
   T num_b;

   Addition(T const*, T const*) // by T const*, not T*
   {
      int temp_a = a, temp_b = b;
      a = char[N], b = char[N]; 
      // converting
   }
public:
   Addition(T const&, T const&) // by T const&, not T
   :
       Addition(&a, &b) // delegate to the other constructor
   {} 
}; // <-- make sure to end class definitions with a semi-colon!

これにより、すべてのメンバー関数の宣言と定義の両方を面倒に書く必要がなくなります。短くて甘いクラス (とにかく努力する必要があります) の場合、これはテンプレートを記述する好ましい方法ですが、非常に長い定義の場合は、宣言と定義を分離することをお勧めします。

最後に、@tacp で説明されているようにthis->a、関数パラメーターからクラス データ メンバーを明確にするために使用する必要があります。そのため、多くの場合、末尾にアンダースコアまたはm_プレフィックスを付けてデータ メンバーを記述します。

于 2013-04-13T16:12:56.013 に答える