1

作为模板编程的新手,我迷失在我正在尝试构建的结构中。

class Base;

template <typename T>
class Foo : public Base {
protected:
    T mValue;
    std::vector<Base> mFooVector;   // the idea is to have Foo<T> elements
                                    // of several T types

public:
    T value() {
        return mValue;
    }

    void SetValueFromVector() {
        mValue = Transform( mFooVector );
        // to give you an idea of what awaits below
    }

    T Transform( std::vector<Base> ) {
        // Question deals with what I want to do here
    }

    /*...*/
}

class Base{
    // here, how to declare a virtual or template 'value()'
    // that could be instantiated or specialized within Foo?
    // Or should I declare a public 'mValue' of some type?
    // (see below)
};

Transform我想从“mFooVector”的所有元素中调用 value(),这些元素可以排列成各种类型的关联。将有在别处定义的函数来处理所有可能的实际情况。但是仍然存在如何调用value()或访问mValue声明为类实例的对象的问题Base

我昨天问了一个问题,这只是我研究的开始——问题不再是容器,因为我使用了多态性,而是我需要value()在它包含的实例上调用函数。

编辑:如果它可以帮助任何人,这是我最终按类定义的方式。我接受了@PiotrNycz 的回答,因为它让我非常接近我的目标,但它并没有完全实现我所需要的。

class Base {
public:
   virtual void setValue( &int ) =0;
   virtual void setValue( &float ) =0;
   virtual void setValue( &double ) =0;
}

template <typename T>
class Foo : public Base {
protected:
    T mValue;
    std::vector<Base*> mFooVector;
    T Transform( std::vector<Base*> );
public:

    // this is for use from outside the class
    T value() {
       return mValue;
    }

    void SetValueFromVector() {
       mValue = Transform( mFooVector );
    }

    // the three below are only going to be used within 'Transform'
    void setValue( int& i ) { i = mValue; }
    void setValue( float& f ) { f = (float) mValue; }
    void setValue( double& d ) { d = (double) mValue; }
}

// specialization of Transform
template<>
int Foo<int>::Transform( std::vector<Base*> v )
{
    // here I only use setValue functions
    // if for example I know that first v element is 'float'
    float val;
    v[0]->setValue( val );

    return 3*int(val);
}
// and etc.
4

1 回答 1

0

我假设您知道所有可能的值类型,假设它们是int/float/...,那么您的基类必须准备好(通过纯虚拟方法)添加到任何值类型:

class Base {
public:
  virtual ~Base() {}
  virtual void addTo(int& value) = 0;
  virtual void addTo(long& value) = 0;
  virtual void addTo(float& value) = 0;
  virtual void addTo(double& value) = 0;
  // add as many functions as you need and of types you need in your design
  // these types not necessarily must be simple basic types
};

template <class Type> 
class Foo : public Base {
public:
    T Transform( std::vector<Base> ) {
        T retVal = T();
        // Question deals with what I want to do here
        for (auto it = mFooVector.begin(); it != mFooVector.end(); ++i)
          it->addTo(retVal);
        return retVal;
    }
  // and the base interface implementation 
  // (for other types implementation does not need to be some simple)
  virtual void addTo(int& value) { value += mValue; }
  virtual void addTo(long& value) { value += mValue; }
  virtual void addTo(float& value) { value += mValue; }
  virtual void addTo(double& value) { value += mValue; }

private:
  std::vector<std::shared_ptr<Base>> mFooVector;
  // you can here ^^^^^^^^^^^^^^^^^ use simple Base* pointer but must 
  // then define d-tor, copy c-tor and assignment operator
  T mValue;  
};
于 2012-10-20T10:50:47.023 回答