Check if class is derived from a specific class (compile, runtime both answers available)

后端 未结 6 1857
不知归路
不知归路 2020-12-03 12:13

It is easier to explain on an example so,

class base {
//....
}

class derived1 : public base {
//...
}

In my library, there is a pointer o

6条回答
  •  生来不讨喜
    2020-12-03 12:32

    The compiler will only accept pointers to classes derived from your base class if your library functions takes pointers to the base class. My answer is with a classical approach type safety will handle it. To my experience that kind of type checking is enough. Having 25 years experience in the industry I question the need to do this check. Maybe such a fundamental question is not welcome? I would like to see the justification to having the need to do this kind of upcast. I never have to do that. The opposite, i.e. a down cast I need quite frequently.

提交回复
热议问题