virtual

Why vptr is not static?

时光毁灭记忆、已成空白 提交于 2019-11-30 05:02:21
Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ? Instead of associating the vptr with the object, why not associate it with the class ? The runtime class of the object is a property of the object itself. In effect, vptr represents the runtime class, and therefore can't be static . What it points to, however, can be shared by all instances of the same runtime class. Your diagram is wrong. There is

Why C++ virtual function defined in header may not be compiled and linked in vtable?

懵懂的女人 提交于 2019-11-30 04:57:47
问题 Situation is following. I have shared library, which contains class definition - QueueClass : IClassInterface { virtual void LOL() { do some magic} } My shared library initialize class member QueueClass *globalMember = new QueueClass(); My share library export C function which returns pointer to globalMember - void * getGlobalMember(void) { return globalMember;} My application uses globalMember like this ((IClassInterface*)getGlobalMember())->LOL(); Now the very uber stuff - if i do not

Does overriding a non-const virtual method hide a const overload?

不打扰是莪最后的温柔 提交于 2019-11-30 04:40:22
问题 Consider: #include <iostream> using namespace std; struct A { virtual void f() { cout << "A::f" << endl; } virtual void f() const { cout << "A::f const" << endl; } }; struct B : public A {}; struct C : public A { virtual void f() { cout << "C::f" << endl; } }; int main() { const B b; b.f(); // prints "A::f const" const C c; c.f(); // Compile-time error: passing ‘const C’ as ‘this’ argument of // ‘virtual void C::f()’ discards qualifiers } (I'm using GCC.) So it seems that the const version of

设计模式4-装饰器模式Decorator

不羁岁月 提交于 2019-11-30 04:29:14
装饰器模式属于"单一职责"模式. 在软件组件的设计中,如果责任划分不清晰,使用继承得到的结果,往往是随着需求的变化,子类 急剧膨胀 ,同时充斥着 重复代码 ,这是代码的bad smell. 典型的单一职责模式:Decorator,Bridge.这两种模式表现出了很强的"单一职责模式的味道".本文中我们讨论Decorator模式. 动机 在某些情况下,我们可能会"过度的使用继承来扩展对象的功能",由于继承为类型引入了静态特质,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀. 如何使 对象功能的扩展 能够根据需要动态实现,同时避免 扩展功能的增多 带来的子类膨胀问题?从而使得任何 功能扩展变化 所带来的影响最低. 实例 考虑设计一个IO的库,主要是一些流操作.可以首先定义一个基类Stream, //业务操作 class Stream{ public: virtual char Read(int number)=0; virtual void Seek(int postion)=0; virtual void Write (char data)=0; ~Stream() { } }; 基于Stream可以定义文件流,网络流,内存流等派生类,然后重写实现基类的成员函数. class FileStream:public

Virtual base class data members

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 04:21:58
问题 Why it is recommended not to have data members in virtual base class? What about function members? If I have a task common to all derived classes is it OK for virtual base class to do the task or should the derived inherit from two classed - from virtual interface and plain base that do the task? Thanks. 回答1: As a practice you should only use virtual inheritance to define interfaces as they are usually used with multiple inheritance to ensure that only one version of the class is present in

How to override the <ENTER> key behaviour of the virtual keyboard in Android

懵懂的女人 提交于 2019-11-30 04:15:19
问题 I want to override the behaviour of the ENTER key of the virtual keyboard so that: when there are more fields on the screen, it 'tabs' to the next field when it is the last field of the screen, it performs the default action of the screen I've been playing with the IME options and labels, but just don't get what I want. Anybody have any suggestions? 回答1: With help on another forum, I found the way to do it. To make it reusable, I have created my own super dialog class that contains 2

Alternatives to vtable

戏子无情 提交于 2019-11-30 03:51:47
问题 Vtables are ubiquitous in most OO implementations, but do they have alternatives? The wiki page for vtables has a short blurb, but not really to much info (and stubbed links). Do you know of some language implementation which does not use vtables? Are there are free online pages which discuss the alternatives? 回答1: Yes, there are many alternatives! Vtables are only possible when two conditions hold. All method calls can be determined statically. If you can call functions by string name, or if

Out-of-Line Virtual Method

我的梦境 提交于 2019-11-30 02:44:23
What exactly is a out-of-line virtual method and why does it affect link times? http://llvm.org/docs/CodingStandards.html says If a class is defined in a header file and has a vtable (either it has virtual methods or it derives from classes with virtual methods), it must always have at least one out-of-line virtual method in the class. Without this, the compiler will copy the vtable and RTTI into every .o file that #includes the header, bloating .o file sizes and increasing link times. The compiler must emit a vtable for classes with virtual methods. This contains the pointers to these methods

Why is the “virtuality” of methods implicitly propagated in C++?

假装没事ソ 提交于 2019-11-30 01:50:14
问题 What is the reason for removing the ability to stop the propagation of methods virtuality? Let me be clearer: In C++, whether you write "virtual void foo()" or "void foo()" in the derived class, it will be virtual as long as in the base class, foo is declared virtual. This means that a call to foo() through a derived* pointer will result in a virtual table lookup (in case a derived2 function overrides foo), even if this behavior is not wanted by the programmer. Let me give you an example

动态内容缓存技术 CSI,SSI,ESI介绍

ぃ、小莉子 提交于 2019-11-30 01:44:15
1.CSI方案 CSI (Client Side Includes) 通过iframe、javascript、ajax 等方式将另外一个页面的内容动态包含进来。 2.SSI方案 SSI (Server Side Includes) Server Side Includes (SSI) is a simple interpreted server-side scripting language used almost exclusively for the Web. 在HTML文件中,可以通过注释行调用的命令或指针。实现整个网站的内容更新。SSI需要特殊的文件后缀(shtml,inc)。如: <!--#include virtual="../date.jsp" --> 3.ESI方案 ESI (Edge Side Includes) Edge Side Includes(ESI) 和Server Side Includes(SSI)和功能类似。ESI(Edge Side Include)通过使用简单的标记语言来对那些可以加速和不能加速的网页中的内容片断进行描述,每个网页都被划分成不同的小部分分别赋予不同的缓存控制策略,使Cache服务器可以根据这些策略在将完整的网页发送给用户之前将不同的小部分动态地组合在一起。通过这种控制,可以有效地减少从服务器抓取整个页面的次数