c++ - 在模板类中,如何处理基类的未指定对象数组?

标签 c++ templates polymorphism containers

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

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(),这些元素可以排列成各种类型关联。将有其他地方定义的函数来处理所有可能的实际情况。 但是仍然存在如何从声明为类 Base 实例的对象调用 value() 或访问 mValue 的问题。

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

编辑:如果它对任何人有帮助,这是我最终按类(class)定义的方式。我接受了@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.

最佳答案

我假设您知道所有可能的值类型,假设它们是 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;  
};

关于c++ - 在模板类中,如何处理基类的未指定对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12985735/

相关文章:

c++ - 无法在循环中实例化泛型函数

c++ - c++中最难发现的错误

c++: vector<Base> 可以包含 Derived 类型的对象吗?

c++ - 从 C++ 中的基类返回派生的 "this"的多态性

c++ - "run time templates"

c++ - GDB 显示没有来自带有调试符号的应用程序的回溯

c++ - 如何以类似于 C 函数的方式将 dlsym 映射到非静态 C++ 成员函数?

c++ - 返回模板类的模板函数 - gcc 编译问题 - 错误 : expected unqualified-id before '<' token

c++ - 指向具有模板成员的结构的不透明指针

c++ 模板队列类的嵌套初始化