在模板类中,如何处理基类的未指定对象的数组
In a template class, how to handle unspecified arrays of objects of a base class?
是模板编程的新手,我在试图构建的结构中迷路了。
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
。
我昨天问了一个问题,这只是我的研究的开始 - 问题不再是容器,因为我使用了多态性,而是我需要在其中包含的情况下调用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.
我假设您知道所有可能的价值类型,假设它们是 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;
};
相关文章:
- std::具有相同基类的类的变体
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- 在同时处理基类的多个指针时如何处理多态性?
- 如何处理参数中的基类和指针变量
- 使用 enable_if 专门处理基类的返回类型
- 编译器如何处理基类破坏者在派生的破坏者中调用
- 如何使派生类的流式处理运算符也输出基类
- 如何使继承相同基类的类的虚拟方法弄清楚如何处理它们的参数?
- 处理C++中基类的重写方法的异常
- SWIG:在派生类中处理基类重载方法
- 在模板类中,如何处理基类的未指定对象的数组
- 重写类函数并调用它,尽管它是作为基类处理的
- 使用基类而不是基指针来处理派生类
- 将抽象基类作为成员处理时的所有权问题
- c++处理跨超/基类的函数指针
- 如何处理基类函数已经重载不同访问权限时的名称隐藏问题
- c++:为什么可以编译?(处理this-Reference到基类)
- 用于智能指针(intrusive_ptr)的抽象基类-处理继承性、多态性、可克隆性和从工厂返回的方法
- 事件处理:当接收者期望派生类时,从基类发送事件
- c++基类中的异常处理