Root base class in C++

前端 未结 7 1476
逝去的感伤
逝去的感伤 2021-01-04 11:29

Every object in .NET inherits (directly or indirectly) from the common root base \"Object\". Is there such a common object root in C++? How do I pass any object to

相关标签:
7条回答
  • 2021-01-04 12:21

    Template functions are present and avoid the need for such root parent of all classes.

    template <class T>
    void DoSomeStuff(T const &t) {
        // Do the stuff with t...
        t.callTheFunction();
    }
    

    If all your objects have a member function callTheFunction(), then you got the exactly same behavior than having a root base class, with the same requirement (all your classes have a function with that name).

    In addition, you got the additional benefit of being able to specialize the template function DoSomeStuff() for some classes that are not yours, and could not inherit your virtual member function.

    0 讨论(0)
提交回复
热议问题