6

私は単一のクラス「ベース」と、ベースから派生した数十のクラスを持っています。インデックスによって適切なクラスを作成するメソッドが必要です。このような:

class Base
{
};

class A : public Base
{
}

class B : public Base
{
}

class C : public Base
{
}

Type array = { A, B, C };

そして、私はできるnew array[i];

これは C++(0x) でどのように達成できますか? 通常、Abstract Factory パターンを使用します。しかし、私はたくさんの派生クラスを持っているので、これは本当にプログラムを遅くします。

派生クラスは一度だけ使用されるため、これを使用することも教えました。

Base *array = { new A, new B, new C };

ただし、すべてのクラスが常に使用されるわけではないことを除けば、これは大量のメモリ消費につながります。

なにか提案を?

4

2 に答える 2

11

クラスの配列は使用できませんが、関数へのポインターの配列は使用できます。

typedef std::unique_ptr<Base> (*Creator)();

template <typename T>
std::unique_ptr<Base> make() { return new T{}; }

Creator const array[] = { make<A>, make<B>, make<C> };

int main() {
    std::unique_ptr<Base> b = array[1]();

    b->foo();
}

非常に多くのテンプレート関数を作成するコストが心配な方のために、以下に例を示します。

#include <stdio.h>

struct Base { virtual void foo() const = 0; };

struct A: Base { void foo() const { printf("A"); } };
struct B: Base { void foo() const { printf("B"); } };
struct C: Base { void foo() const { printf("C"); } };


typedef Base* (*Creator)();

template <typename T>
static Base* make() { return new T{}; }

static Creator const array[] = { make<A>, make<B>, make<C> };

Base* select_array(int i) {
    return array[i]();
}

Base* select_switch(int i) {
    switch(i) {
    case 0: return make<A>();
    case 1: return make<B>();
    case 2: return make<C>();
    default: return 0;
    }
}

LLVM/Clang は次の出力を生成します。

define %struct.Base* @select_array(int)(i32 %i) uwtable {
  %1 = sext i32 %i to i64
  %2 = getelementptr inbounds [3 x %struct.Base* ()*]* @array, i64 0, i64 %1
  %3 = load %struct.Base* ()** %2, align 8, !tbaa !0
  %4 = tail call %struct.Base* %3()
  ret %struct.Base* %4
}

define noalias %struct.Base* @select_switch(int)(i32 %i) uwtable {
  switch i32 %i, label %13 [
    i32 0, label %1
    i32 1, label %5
    i32 2, label %9
  ]

; <label>:1                                       ; preds = %0
  %2 = tail call noalias i8* @operator new(unsigned long)(i64 8)
  %3 = bitcast i8* %2 to i32 (...)***
  store i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*]* @vtable for A, i64 0, i64 2) to i32 (...)**), i32 (...)*** %3, align 8
  %4 = bitcast i8* %2 to %struct.Base*
  br label %13

; <label>:5                                       ; preds = %0
  %6 = tail call noalias i8* @operator new(unsigned long)(i64 8)
  %7 = bitcast i8* %6 to i32 (...)***
  store i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*]* @vtable for B, i64 0, i64 2) to i32 (...)**), i32 (...)*** %7, align 8
  %8 = bitcast i8* %6 to %struct.Base*
  br label %13

; <label>:9                                       ; preds = %0
  %10 = tail call noalias i8* @operator new(unsigned long)(i64 8)
  %11 = bitcast i8* %10 to i32 (...)***
  store i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*]* @vtable for C, i64 0, i64 2) to i32 (...)**), i32 (...)*** %11, align 8
  %12 = bitcast i8* %10 to %struct.Base*
  br label %13

; <label>:13                                      ; preds = %9, %5, %1, %0
  %.0 = phi %struct.Base* [ %12, %9 ], [ %8, %5 ], [ %4, %1 ], [ null, %0 ]
  ret %struct.Base* %.0
}

残念ながら、通常の配列コードを使用して関数を自動的にインライン化するほどインテリジェントではありません (LLVM オプティマイザーの既知の問題、gcc の方が優れているかどうかはわかりません)... しかし、それを使用switchすることは実際に可能です。

于 2012-05-23T15:37:02.797 に答える
5
typedef Base* BaseMaker();

template <class X> Base* make() {
  return new X;
}

BaseMaker* makers[] = { make<A>, make<B>, make<C> };

Base* b = makers[2]();
于 2012-05-23T15:35:35.890 に答える