简体   繁体   English

C++ 虚方法

[英]C++ Virtual Method

If I create a struct:如果我创建一个结构:

struct joinpoint_exception: exception
{

   virtual const char* what () const throw ();
};

What does what () const throw () means in this context? what () const throw ()在这种情况下是什么意思?

what is a virtual member function returning a pointer to constant char which is itself constant and throws nothing. what是虚拟成员 function 返回指向常量char的指针,该指针本身是常量并且什么都不抛出。

virtual const char* what () const throw ();
|-----| <- virtual member function
        |---------| <- returning a pointer to constant chars
                    |-----| <- named what
                            |---| <- which is constant
                                  |-------| <- which does not throw

(Technically the function can still throw, but if it does, it goes directly to std::unexpected , which defaults to calling std::terminate ) (从技术上讲,function 仍然可以抛出,但如果确实如此,它会直接转到std::unexpected ,默认调用std::terminate

what is name of the method方法的名称是what

const means that the method does not alter any internal data unless its mutable const意味着该方法不会改变任何内部数据,除非它是mutable

throw () means that the method should not throw an exception, if it does std::unexpected is thrown instead throw ()表示该方法不应该抛出异常,如果它抛出std::unexpected则相反

It means that what() is const (ie it won't modify the logical state of the object) and that it doesn't throw any exceptions (as indicated by throw() ).这意味着what()const (即它不会修改对象的逻辑 state)并且它不会抛出任何异常(如throw()所示)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM