简体   繁体   English

从模板派生类访问非模板基类虚函数

[英]accessing non-template base class virtual function from template derived class

I'm trying to understand the template and inheritance. 我试图了解模板和继承。 I have a base class as follows: 我有一个基类如下:

class Base
{
public:
    virtual ~Base() {}
    virtual void setId(u_int8_t id)
    {
        m_id = id;
    }

private:
    u_int8_t m_id;
};

and a derived template class as follows: 以及派生的模板类,如下所示:

template <typename T>
class Data : public Base
{
public:
    virtual void setId(u_int8_t id)
    {
        this->setId(id);
    }

    inline void setData(const T& data)
    {
        m_data = data;
    }

    inline T& data()
    {
        return m_data;
    }

private:
    T m_data;
};

this code compiles fine but crashes at run time. 此代码可以正常编译,但在运行时崩溃。 Why is this happening? 为什么会这样呢?

You get a stack overflow because setId keeps calling itself. 由于setId不断调用自身,因此会导致堆栈溢出。 To call the setId in the base class, use 要在基类中调用setId ,请使用

virtual void setId(u_int8_t id)
{
    Base::setId(id);
}

This function: 该功能:

virtual void setId(u_int8_t id)
{
    this->setId(id);
}

It is calling itself recursively, until the process runs out of stack space and you get a crash. 它以递归方式调用自己,直到进程用尽堆栈空间并导致崩溃。

To call a function from the base-class you have to use the scope operator: 要从基类中调用函数,必须使用范围运算符:

Base::setId(id);

The setId() function recursively calls itself forever. setId()函数永远递归调用自己。 You want: 你要:

virtual void setId(u_int8_t id)
{
     Base::setId(Id);
}

You don't actually need setId(u_int8_t id) in Data . 实际上,您在Data不需要setId(u_int8_t id) The method is inherited from Base . 该方法是从Base继承的。 If you are intending to provide a different implementation in derived class, and use in this different implementation the implementation of the Base , then use Base::setId(id) (as Joachim Pileborg pointed out) 如果您正打算提供派生类不同的实现,并使用在此不同实施的实施Base ,然后利用Base::setId(id)如约阿希姆Pileborg指出)

PS: Actually, there is nothing specific to templates in your question. PS:实际上,您的问题中没有专门针对模板的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从非模板基类派生的模板类:无法访问基类变量? - Template Class derived from Non-template base class: No access to base class variables? 继承的非模板类的范围问题(模板基,非模板派生) - Scope problems with inherited non-template class (template base, non-template derived) 将非模板基类向下转换为模板化派生类:是否可能? - Downcasting non-template base class to templated derived class: is it possible? 从基非模板类调用派生模板类方法 - Calling derived template class method from base non-template class 在从模板化中介派生的 class 中调用非模板基 class 构造函数 - Calling non-template base class constructor in class derived from templated intermediary 从非模板基 class 指针调用模板派生 class 方法的方法 - Invoke a method of templated derived class method from non-template base class pointer 从非模板库中派生模板类 - Deriving a template class from a non-template base 如何使用非模板派生的 class 制作非类型模板基础 class - How to make a non-type template base class with non-template derived class 模板类从非模板基类扩展并覆盖函数参数 - Template class extending from non-template base class and overriding function parameters 在派生的模板类中从基类重新实现虚函数 - Reimplement a virtual function from base class in a derived template class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM