virtual

Virtual method tables

别等时光非礼了梦想. 提交于 2019-11-27 04:04:42
问题 When discussing sealed classes, the term "virtual function table" is mentioned quite frequently. What exactly is this? I read about a method table a while ago (I don't remember the purpose of the purpose of this either) and a google/search on here brings up C++ related results. Thanks 回答1: The C# virtual function table works basically the same as the C++ one, so any resources which describe how the C++ virtual function table works should help you pretty well with the C# one as well. For

Writing a Virtual Printer in .NET [closed]

纵饮孤独 提交于 2019-11-27 02:58:33
I'm looking to create a virtual printer that passes data to my .NET application. I want to then create an installer that installs both the printer and the .NET application. It would we really nice to be able to write it all in C#, but I have a feeling that this will require a printer driver to be written is unmanaged code. Does anyone know of a fairly clean tutorial or example of how to do this? Visit http://www.printerplusplus.com . It is open source .NET virtual printer. It gives you an installer and a .NET class for writing code to process your "printer data". Did exactly what you are

Calling derived class function from base class

强颜欢笑 提交于 2019-11-27 02:39:53
问题 class base { public: virtual void start(); virtual void stop(); void doSomething() { start(); .... stop(); } } class derived : public base { public: void start(); void stop(); } But when I call doSomething() in the derived class it is using it's own definition of Start() and Stop() - not the derived ones. I don't want to rewrite doSomething() in the derived class because it would be identical to the base one. What am I doing wrong? Sorry if that wasn't clear. The behaviour of Start() and Stop

C++ inherit from multiple base classes with the same virtual function name

梦想的初衷 提交于 2019-11-27 02:14:20
I tried this code: class A { virtual void foo() = 0; }; class B { virtual void foo() = 0; }; class C : public A, public B { //virtual void A::foo(){} //virtual void B::foo(){} virtual void A::foo(); virtual void B::foo(); }; void C::A::foo(){} void C::B::foo(){} int main() { C c; return 0; } It is OK when using the commented part, but when I try to write the definitions outside the class declaration, the compiler reports errors. I am using the MSVC11 compiler, does anyone know how to write this? I need to move the code into the cpp file. Thank you~~ dyp A function overrides a virtual function

How can C++ virtual functions be implemented except vtable? [duplicate]

假装没事ソ 提交于 2019-11-27 02:12:02
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: A question about virtual mechanism in C++ Is using vtable the only way to implement virtual member functions mechanism in C++? What other ways exist? 回答1: Another known mechanism is type dispatch functions. Effectively, you replace the vtable pointer by a typeid (small enum). The (dyanmic) linker collects all overrides of a given virtual function, and wraps them in one big switch statement on the typeid field.

override on non-virtual functions

左心房为你撑大大i 提交于 2019-11-27 02:04:32
问题 The C++11 FDIS it says If a virtual function is marked with the virt-specifier override and does not override a member function of a base class, the program is ill-formed. [ Example: struct B { virtual void f(int); }; struct D : B { void f(long) override; // error: wrong signature overriding B::f void f(int) override; // OK }; What if B::f would not have been marked virtual? Is the program ill-formed, then? Or is override then to be ignored`. I can not find any handling of this case in the

Number of Virtual tables and Virtual Pointers in a C++ Program

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 01:41:24
问题 Let say we have below program: class A { public: virtual fun(){}; }; class B:public A { public: virtual fun(){}; }; int main() { A a1; B b1; } My question is how many vtables and how many vptrs will be created, when we run this program? 回答1: Its heavily implementation dependent, but generally you'll get one vtable object per class that has any virtual functions (classes with no virtual functions or bases don't need them), and one vptr per object of a class with a vtable (pointing at the class

C++ 外部调用private方法

你离开我真会死。 提交于 2019-11-27 01:30:19
1、思考,对于C++,能不能在外部调用私有方法? 2、在Java中,子类继承不能缩小父类成员的访问权限。因为在Java中,继承只是表示Is-A关系,因此,父类提供的接口,子类必须承诺仍然提供,不能缩小访问权限,但是可以放大权限。 3、在C++中,继承不光表示Is-A关系,还可以表示根据某物实现出。因此,在C++中,子类可以放大或者缩小父类成员的访问权限。考虑,如果父类提供public virtual方法,子类重写为private virtual方法,我们知道,重写只不过是,子类整体拷贝父类的虚方法表,对于重写的方法偷梁换柱,替换为重写后的方法。在这种情况下,父类指针指向子类对象,父类指针通过调用public virtual方法,动态绑定到子类的private virtual方法。 转载于:https://www.cnblogs.com/nzbbody/p/3523127.html 来源: https://blog.csdn.net/weixin_30719711/article/details/99234298

对象占用空间

℡╲_俬逩灬. 提交于 2019-11-27 01:25:35
1、对象分为字段和方法,字段分为:static,non-static。方法分为static,non-static,virtual 2、static字段,放在静态存储区,所有的对象共享。non-static字段,每个对象拥有自己的一份内存copy 3、static方法,non-static方法,virtual方法都放在代码区,所有的对象共享。调用方法的时候,如何区分方法内的数据是那个对象的呢?   方法内的数据是方法绑定对象的数据,因为调用方法的时候,需要指定对象。对于virtual方法,还要增加一个额外指针,用于在运行时,根据对象的真实类型绑定方法。注意:就算有多个virtual方法,也只有一个额外指针。 4、因此,对象占用的空间大小就是:   不存在virtual方法:就是实例字段的大小   存在virtual方法(一个或多个)就是:实例字段的大小+virtual方法的额外指针 转载于:https://www.cnblogs.com/nzbbody/p/3382973.html 来源: https://blog.csdn.net/weixin_30263277/article/details/99234465

理解virtual方法

别说谁变了你拦得住时间么 提交于 2019-11-27 01:22:01
1、使用场景   virtual方法的使用场景:父类告诉子类,继承接口,修改实现,从而可以面向接口编程。   non-virtual方法的使用场景:父类告诉子类,继承接口和实现,从而可以代码复用。 2、成员方法是一种封装技术,暴露给程序员。对于编译器而言,没有成员方法的概念,编译器会把成员方法编译为普通方法,方法的拥有者(也就是对象)转化为普通方法的形参,这个形参是const指针,名称为this,指向的类型是方法拥有者的类型。 3、编译器编译的时候,只知道指针的表面类型,正是这个表面类型引导编译器去解释指向对象的大小和内容,那么问题来了?   对于non-virtual方法,子类继承接口和实现,继承的方法中,this指针的表面类型是什么? 答案是:Base   对于virtual方法,子类继承了接口,修改了实现,重写的方法中,this指针的表面类型是什么?答案是:Derived 4、思考一下,运行时多态,为什么一定要标记一下方法是virtual?   考虑,Base和Derived都有一个方法Say,形参表一样(这里会发生隐藏)。   Base::Say() 转化为Say(Base* const this),   Derived::Say()转化为Say(Derived* const this),   Base* pb = new Derived();   现在调用pb->Say(