Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套C+中的引用类型+;模板 我对这个问题感到困惑,用C++模板。_C++_Templates_Inheritance_Syntax - Fatal编程技术网

嵌套C+中的引用类型+;模板 我对这个问题感到困惑,用C++模板。

嵌套C+中的引用类型+;模板 我对这个问题感到困惑,用C++模板。,c++,templates,inheritance,syntax,C++,Templates,Inheritance,Syntax,我有一个定义如下的类: template <class T> class DataConsumer { public: DataConsumer() : processCount(0) {} ~DataConsumer() {} // Sub classes override this in order to do their thing void preConsume() { std::cout <

我有一个定义如下的类:

template <class T>
class DataConsumer
{
public:

    DataConsumer() : processCount(0)
    {}

    ~DataConsumer()
    {}

    // Sub classes override this in order to do their thing
    void preConsume()
    {
        std::cout << "Preconsume called" << std::endl;
        processCount++;
    }

    virtual void consume(const T *dataSource)
    {
        preConsume();
    }

private:

    long long processCount; // The bare bones template keeps track of the number of data frames it has consumed
};
模板
类数据消费者
{
公众:
DataConsumer():进程计数(0)
{}
~DataConsumer()
{}
//子类为了完成它们的任务而重写它
无效先决条件()
{
std::cout meta=existingFrame.meta;
}
~DataSource()
{}
//更新函数。专门化在此基础上展开
无效更新()
{}
//返回指向某物的常量指针(数据源可以是图像帧、文本、二进制等)
常量T*getCurrentDataFrame()常量
{
const T*const frame_ptr=currentFrame.get();//只返回一个不可修改的指针
返回帧ptr;
}
void echo()
{
//通用数据源只打印其当前帧
std::cout frame meta-toString()
也许用汽车

    auto frame = getCurrentDataFrame();
“type_traits”技术基本上具有公共类型定义。类似于:

template<typename T>
struct DataFrame { };

template<typename T>
class DataSource
{
    public:           
       typedef DataFrame<T> frame_type;
       //using frame_type = DataFrame<T>; // c++11
};

template<typename T>
virtual void consume(const T *dataSource) // imagine T is DataSource
{
    preConsume();
    T::frame_type * frame = getCurrentDataFrame();
    std::cout << frame->toString() << std::endl; // How do I do this?
}
模板
结构数据帧{};
模板
类数据源
{
公众:
typedef数据帧帧_类型;
//使用frame_type=DataFrame;//c++11
};
模板
虚空消耗(const T*dataSource)//假设T是dataSource
{
消费前();
T::frame_type*frame=getCurrentDataFrame();
std::cout-toString()
也许用汽车

    auto frame = getCurrentDataFrame();
“type_traits”技术基本上具有公共类型定义。类似于:

template<typename T>
struct DataFrame { };

template<typename T>
class DataSource
{
    public:           
       typedef DataFrame<T> frame_type;
       //using frame_type = DataFrame<T>; // c++11
};

template<typename T>
virtual void consume(const T *dataSource) // imagine T is DataSource
{
    preConsume();
    T::frame_type * frame = getCurrentDataFrame();
    std::cout << frame->toString() << std::endl; // How do I do this?
}
模板
结构数据帧{};
模板
类数据源
{
公众:
typedef数据帧帧_类型;
//使用frame_type=DataFrame;//c++11
};
模板
虚空消耗(const T*dataSource)//假设T是dataSource
{
消费前();
T::frame_type*frame=getCurrentDataFrame();

std::cout-toString()通常在模板内部创建
typedef
,以别名其基础类型和依赖类型

template <class T>
class DataSource
{
public:
    typedef T value_type;
    typedef value_type const * pointer_to_const_type;
    // ....  
    // Return a const pointer to something (the data source can be an image frame, textual, binary etc.)
    pointer_to_const_type getCurrentDataFrame() const
    // .....

另外,如果您可以使用c++11标准,您可以使用
auto
关键字,而不是
typename DataSource::pointer\u to_const\u type
作为@Phantom points。

通常在模板内部创建
typedef
以别名其基础类型和依赖类型

template <class T>
class DataSource
{
public:
    typedef T value_type;
    typedef value_type const * pointer_to_const_type;
    // ....  
    // Return a const pointer to something (the data source can be an image frame, textual, binary etc.)
    pointer_to_const_type getCurrentDataFrame() const
    // .....
另外,如果您可以使用c++11标准,您可以使用
auto
关键字,而不是
typename DataSource::pointer\u to_const\u type
作为@Phantom points。

类型特征:

template <class T> DataSource {
public:
    typedef T element_type;
};
template <class T> DataConsumer {
public:
    void consume(T *arg) {
      typename T::element_type local_var;
      ...
    }
};
模板数据源{
公众:
类型定义T元素_类型;
};
模板数据消费者{
公众:
无效消耗(T*arg){
typename T::element\u type local\u var;
...
}
};
使用C++11 decltype:

template <class T> DataSource {
};
template <class T> DataConsumer {
public:
    void consume(T *arg) {
      decltype(arg->getCurrentDataFrame()) local_var;
      ...
    }
};
模板数据源{
};
模板数据消费者{
公众:
无效消耗(T*arg){
decltype(arg->getCurrentDataFrame())本地变量;
...
}
};
具有模板成员功能:

template <class T> DataSource {
};
template <class T> DataConsumer {
public:
    template <class X> void consume(DataSource<X> *arg) {
      X local_var;
      ...
    }
};
模板数据源{
};
模板数据消费者{
公众:
模板无效消耗(数据源*arg){
X局部变量;
...
}
};
如果只想捕获函数调用的返回,也可以使用C++11 auto:

template <class T> DataSource {
};
template <class T> DataConsumer {
public:
    void consume(T *arg) {
      auto local_var = arg->getCurrentFrame();
      ...
    }
};
模板数据源{
};
模板数据消费者{
公众:
无效消耗(T*arg){
自动本地_var=arg->getCurrentFrame();
...
}
};
具有类型特征:

template <class T> DataSource {
public:
    typedef T element_type;
};
template <class T> DataConsumer {
public:
    void consume(T *arg) {
      typename T::element_type local_var;
      ...
    }
};
模板数据源{
公众:
类型定义T元素_类型;
};
模板数据消费者{
公众:
无效消耗(T*arg){
typename T::element\u type local\u var;
...
}
};
使用C++11 decltype:

template <class T> DataSource {
};
template <class T> DataConsumer {
public:
    void consume(T *arg) {
      decltype(arg->getCurrentDataFrame()) local_var;
      ...
    }
};
模板数据源{
};
模板数据消费者{
公众:
无效消耗(T*arg){
decltype(arg->getCurrentDataFrame())本地变量;
...
}
};
具有模板成员功能:

template <class T> DataSource {
};
template <class T> DataConsumer {
public:
    template <class X> void consume(DataSource<X> *arg) {
      X local_var;
      ...
    }
};
模板数据源{
};
模板数据消费者{
公众:
模板无效消耗(数据源*arg){
X局部变量;
...
}
};
如果只想捕获函数调用的返回,也可以使用C++11 auto:

template <class T> DataSource {
};
template <class T> DataConsumer {
public:
    void consume(T *arg) {
      auto local_var = arg->getCurrentFrame();
      ...
    }
};
模板数据源{
};
模板数据消费者{
公众:
无效消耗(T*arg){
自动本地_var=arg->getCurrentFrame();
...
}
};

INSERT_TYPE_HERE*frame=getCurrentDataFrame();
-这应该是
dataSource->getCurrentDataFrame();
吗?就像您的“工作”示例一样?不,这正是我的问题。我想声明一个局部变量,而不是编写dataSource->getCurrentDataFrame()每次。您可以在数据源中声明一个表示模板参数类型(type)的typedef。或者,如果在C++11中使用
decltype
构造。您还可以为
consume
方法设置模板,并捕获模板参数。Jean-Baptiste,这听起来像是我要做的。您可以在回答中扩展您的评论,或者让我看看解释类型特征技术的参考资料吗?
INSERT_type_HERE*frame=getCurrentDataFrame();
-在“工作”示例中应该是
数据源->getCurrentDataFrame();
吗?不,这正是我的问题。我想声明一个局部变量,而不是编写dataSource->getCurrentDataFrame()每次。您可以在数据源中声明一个表示模板参数类型(type)的typedef。或者如果在C++11中使用
decltype
构造。您还可以为
consumer
方法设置模板,并捕获模板参数。Jean-Baptiste,这听起来像是我正在尝试做的。您可以在回答中扩展您的评论,或者让我找到解释类型特征技术的资源吗?如果他可以使用C++11,他也可以告诉我们e
使用
而不是
typedef
:)如果他能使用c++11,他可以使用
使用
而不是
typedef
:)选择此作为答案,因为它是信息量最大的答案选择此作为答案,因为它是信息量最大的答案