Today, I heard a friend come to Tx for an interview and asked about the uncommon C ++ keyword, Khan. I remember I was asked during the interview at that time. The summary is as follows:
Bad_typeid
It is very uncommon. It has not been used in development for several years. To understand the keyword bad_typeid, you must first take a look at the use of the keyword typeid. The following is an English description:
The typeid operator will accept a pointer when dereferenced evaluates to a type: typeid (* P );
If the pointer points to a null value then the typeid operator will throw a bad_typeid exception.
That is to say, when the typeid operator is applied to a null pointer, A bad_typeid exception is thrown. The bad_typeid is essentially a class and is defined in the typeinfo file. See the following example for msdn. Note that at least vs2003 is used for testing. vc6 is not implemented:
// Expre_bad_typeid.cpp <br/> // compile with:/ehs/ gr <br/> # include <typeinfo. h> <br/> # include <iostream> </P> <p> Class A {<br/> public: <br/> // object for class needs vtable <br/> // For rtti <br/> virtual ~ A (); <br/>}; </P> <p> using namespace STD; <br/> int main () {<br/> A * A = NULL; </P> <p> try {<br/> cout <typeid (* ). name () <Endl; // error condition <br/>}< br/> catch (bad_typeid) {<br/> cout <"object is null" <Endl; <br/>}< br/>
Bad_cast
This keyword is similar to bad_typeid. It is an exception thrown when dynamic_cast fails to be converted (generally if the conversion result is a null pointer when the base class is converted to a subclass.
Type of the exceptions thrown by dynamic_cast when they fail the run-time check timed med on references to polymorphic class types.
The run-time check fails if the object wocould be an incomplete object of the destination type.
Its member what returns a null-terminated Character Sequence identifying the exception.
Some functions in the standard library may also throw this exception to signal a type-casting error.
Bad_cast is also a class defined in the typeinfo file. For the following example, use vs2003 at least (vc6 has problems with dynamic_cast implementation:
// Bad_cast example <br/> # include <iostream> <br/> # include <typeinfo> <br/> using namespace STD; </P> <p> class base {virtual void member () {}}; <br/> class derived: Base {}; </P> <p> int main () {<br/> try <br/> {<br/> Base B; <br/> derived & RD = dynamic_cast <derived &> (B); <br/>}< br/> catch (bad_cast & BC) <br/>{< br/> cerr <"bad_cast caught:" <BC. what () <Endl; <br/>}< br/> return 0; <br/>}< br/>
Mutable
The const member function of the class cannot modify the member variables of the class (non-static data member). This is guaranteed by the compiler, But what if I want? Let's use mutable. After modifying member variables with this keyword, the compiler won't bother you. In fact, this keyword is also criticized a lot, there is no conclusion about what to use or when to use it, because this keyword is suspected of damaging the encapsulation of the class. Let's look at a small example:
Class testmutable <br/>{< br/> Public: <br/> tew.utable () <br/>{< br/> m_data = 0; <br/>}</P> <p> int changevalue () const <br/> {<br/> return ++ m_data; <br/>}< br/> PRIVATE: <br/> // int m_data; // compilation error: Error c2166: l-value specifies const object <br/> mutable int m_data; // No problem <br/>}; </P> <p> int main (INT argc, char * argv []) <br/>{< br/> tew.utable tsmutable; <br/> cout <tsmutable. changevalue () <Endl; <br/> cout <tsmutable. changevalue () <Endl; <br/> return 0; <br/>}< br/>
Volatile
This keyword is not described before. See http://blog.csdn.net/magictong/archive/2008/10/29/3175793.aspx.
Explicit
This keyword is sometimes useful. It is used to disable the automatic type conversion of Single-parameter Constructor (usually to convert a basic type into an object, which is obscure ). It is used to modify the class constructor, indicating that the constructor is a displayed constructor, rather than an implicit constructor, the compiler will execute a lot of extra automatic conversions for implicit constructor in case of a value assignment operator. This keyword tells the compiler not to be busy.
Class arrexplicit <br/>{< br/> Public: <br/> // arrexplicit (INT size) // use this sentence, the compiler does not report an error <br/> explicit arrexplicit (INT size) // arrexp = 102 below; the compiler reports an error <br/>{< br/> m_size = size; <br/>}</P> <p> PRIVATE: <br/> int m_size; <br/>}; <br/> int main (INT argc, char * argv []) <br/>{< br/> arrexplicit arrexp (100); <br/> arrexp = 102; // after modification with explicit, compilation error <br/> return 0; <br/>}< br/>