摘要: In C++, RTTI (Run-time type information) is available only for the classes which have at least one virtual function. For example, dynamic_cast uses RTTI and following program fails with error “cannot dynamic_cast `b’ (of type `class B*’) to type `class D*’ (source type is not polymorphic) ” becau... 阅读全文
posted @ 2013-11-26 21:13 虔诚的学习者 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 这个不懂,等看会了再写。。。 阅读全文
posted @ 2013-11-26 21:10 虔诚的学习者 阅读(90) 评论(0) 推荐(0) 编辑
摘要: Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor. Source: https://www.securecoding.cert.org/confluence/display/cplusplus/OOP34-CPP.... 阅读全文
posted @ 2013-11-26 21:07 虔诚的学习者 阅读(347) 评论(0) 推荐(0) 编辑
摘要: Can we make a class constructor virtual in C++ to create polymorphic objects? No. C++ being static typed (the purpose of RTTI is different) language, it is meaningless to the C++ compiler to create an object polymorphically. The compiler must be aware of the class type to create the object. In... 阅读全文
posted @ 2013-11-26 21:01 虔诚的学习者 阅读(466) 评论(0) 推荐(0) 编辑
摘要: We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive access to derived class methods. For example the following program compiles fine. 1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual int fun(int i) 8 { 9 }10 };1... 阅读全文
posted @ 2013-11-26 20:44 虔诚的学习者 阅读(146) 评论(0) 推荐(0) 编辑
摘要: In C++, a static member function of a class cannot be virtual. For example, below program gives compilation error. 1 #include 2 using namespace std; 3 4 class Test 5 { 6 public: 7 // Error: Virtual member functions cannot be static 8 virtual static void fun() 9 { 10 ... 阅读全文
posted @ 2013-11-26 20:32 虔诚的学习者 阅读(353) 评论(0) 推荐(0) 编辑
摘要: In C++, once a member function is declared as a virtual function in a base class, it becomes virtual in every class derived from that base class. In other words, it is not necessary to use the keyword virtual in the derived class while declaring redefined versions of the virtual base class functio.. 阅读全文
posted @ 2013-11-26 20:25 虔诚的学习者 阅读(266) 评论(0) 推荐(0) 编辑
摘要: Predict the output of following C++ program. 1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual void fun ( int x = 0 ) 8 { 9 cout fun();28 return 0;29 } Output: Derived::fun(), x = 0 If we take a closer look at the output, we observe that fun()... 阅读全文
posted @ 2013-11-26 20:19 虔诚的学习者 阅读(190) 评论(0) 推荐(0) 编辑
摘要: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B’s constructor is called before A’s constructor. 1 #include 2 usin... 阅读全文
posted @ 2013-11-26 20:12 虔诚的学习者 阅读(368) 评论(0) 推荐(0) 编辑
摘要: 派生类可以从基类中继承: (1)基类中定义的每个数据成员(尽管这些数据成员在派生类中不一定可以被访问); (2)基类中的每个普通成员函数(尽管这些成员函数在派生类中不一定可以被访问); (3)The same initial data layout as the base class; (4)基类中的static成员函数; 派生类无法从基类中继承: (1)基类的构造函数和析构函数; (2)基类的友元; Please write comments if you find anything incorrect, or you want to share more inf... 阅读全文
posted @ 2013-11-26 19:46 虔诚的学习者 阅读(227) 评论(0) 推荐(0) 编辑