virtual

c++ virtual function call without pointer or reference

落爺英雄遲暮 提交于 2019-12-05 19:51:41
问题 As far as I know, virtual function call usually requires pointer or reference. So I am very surprised by the following codes. #include <iostream> using namespace std; class B{ public: void runB(){ call(); } virtual void call(){ cout<<"B\n"; }; }; class D: public B{ public: void runD(){ runB(); } void call(){ cout<<"D\n"; } }; int main(){ D d; d.runD(); } The output is D Could someone please comment why this virtual function call works? Thanks。 回答1: Within a member function, any references to

多态

醉酒当歌 提交于 2019-12-05 19:07:36
1.继承和虚函数 1)没有继承时虚函数表 Base结构,里面有3个函数:Function1、Function2、Function3; 虚表: 2)单继承无函数覆盖 Base结构: Function1、2、3; Sub结构继承Base: Function4、5、6; 虚表: 子类对象的虚表中包含子类和父类的全部虚函数; 3)单继承有函数覆盖 Base: Function1、2、3; Sub继承Base: Function1、2、6; 虚表: 虚表中包含子类中的全部虚函数和父类中没有被覆盖的虚函数; 4)多继承无覆盖 Base1: Function1、2; Base2: Function3、4; Sub继承了Base1和Base2: Function5、6; 虚表: 有两个虚表; 虚表1中有Base1和Sub中的所有虚函数; 虚表2中有Base2中所有的虚函数; Sub结构相对没有虚函数的结构于多了8个字节; 也就是说,有几个虚表结构中就需要保存几个虚表的地址; 5)多继承有覆盖 Base1: Function1、2; Base2: Function3、4; Sub继承Base1和Base2: Function1、3、5; 虚表: 子类对象覆盖了哪个函数,就将覆盖后的函数的地址放在哪个被覆盖函数的父类的虚表中; 子类特有的函数放第一个虚表中; 6)多重继承无覆盖 Base1:

WHy should virtual methods be explicitly overridden in C#?

孤街浪徒 提交于 2019-12-05 17:41:10
问题 Why should virtual methods be explicitly overridden in C#? 回答1: By declaring a method as virtual , you are stating your intention that the method can be overridden in a derived class. By declaring your implementing method as override , your are stating your intention that you are overriding a virtual method. By requiring that the override keyword be used to override a virtual method, the designers of the language encourage clarity, by requiring you to state your intentions. 回答2: If you don't

CoreMIDI/PGMidi Virtual midi error in iOS6

社会主义新天地 提交于 2019-12-05 17:16:00
问题 Faced with two errors. This code worked in iOS 4 and 5, but after update to 6, it is not working ( I found following, but don't know how to fix it in the code. Beginning in iOS 6, apps need to have the audio key in their UIBackgroundModes in order to use CoreMIDI’s MIDISourceCreate and MIDIDestinationCreate functions. Without the key set, these functions will return kMIDINotPermitted (-10844). 2012-09-23 03:40:04.773 MidiStudio[1017:907] Error (Create MIDI virtual source): -10844:Error Domain

Abstract class : invalid abstract return type for member function ‘virtual…’

寵の児 提交于 2019-12-05 13:43:44
In my program I have such class hierarchy: #include <iostream> #include <cmath> #include <sstream> using namespace std; class aa; class bb; class root { public: virtual ~root() {} virtual root add(const aa& a) const=0; virtual root add(const bb& a) const=0; }; class aa: public root { public: aa() { } aa(const aa& a) { } virtual root add(const aa& a) const { return root(new aa()); } virtual root add(const bb& a) const { return root(new bb()); } }; class bb: public root { public: bb() { } bb(const bb& b) {} virtual root add(const aa& a) const { return root(new bb()); } virtual root add(const bb&

undefined reference to `typeinfo for class' [duplicate]

拟墨画扇 提交于 2019-12-05 13:28:01
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: g++ undefined reference to typeinfo Undefined symbols “vtable for …” and “typeinfo for…”? I can't use my class. class Accel { public: virtual void initialize(void); virtual void measure(void); virtual void calibrate(void); virtual const int getFlightData(byte); }; class Accel_ad : public Accel { public: Accel_ad() : Accel(){} void initialize(void) {/*code code code...*/} void measure(void) {/*measure code*/}

How to create a virtual audio device

点点圈 提交于 2019-12-05 11:52:59
I am working on a project in C# for which I need to create several virtual audio devices. Later I will be doing some switching with them on my own. Does maybe anyone know a nice library that would allow me to do it problematically on my own. To create instances of audio devices. (commercial is OK, but free .dll would be better) Another solution, that would be an optional solution, is to find a software that creates virtual audio devices according to my desires. Mark Heath Unfortunately, you won't be able to create virtual audio devices in C#. You need to create windows device drivers using the

学习记录:《C++设计模式——李建忠主讲》4.“单一职责”模式

徘徊边缘 提交于 2019-12-05 11:23:57
单一职责模式: 在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任。 典型模式: 装饰模式(Decorator)、桥接模式(Bridge)。 一、装饰模式 1.动机 在某些情况下我们可能会“过度地使用继承来扩展对象的功能”,由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀。 2.作用 使“对象功能的扩展”能够根据需要来动态地实现;同时避免“扩展功能的增多”带来的子类膨胀问题,使得任何“功能扩展变化”所导致的影响降为最低。 3.定义 动态(组合)地给一个对象增加一些额外的职责。就增加功能而言,Decorator模式比生成子类(继承)更为灵活(消除重复代码、减少子类个数)。 4.代码 //原有代码 //业务操作 class Stream{ public: virtual char Read(int number)=0; virtual void Seek(int position)=0; virtual void Write(char data)=0; virtual ~Stream(){} }; //主体类 class FileStream: public Stream{ public:

Force the order of functions in the virtual method table?

别等时光非礼了梦想. 提交于 2019-12-05 11:19:42
How can I control the order of virtual functions in the virtual table? Are they laid out in the same order that they are declared in? When inheriting a class with a virtual table, is the virtual table of the inherited class an extension of the base class, or is an entirely new virtual table created with only the inherited classes virtual functions. (i.e. is the virtual table still at index +0x0 of the class?) (a) As far as the standard is concerned, you can't, (in fact you can't even assume that vtables exist). (b) Probably, but what are the circumstances in which you need to control the order

How to call virtual function of derived class through base class pointer

家住魔仙堡 提交于 2019-12-05 08:29:19
Let's see this code: class CBase { public: virtual vfunc() { cout << "CBase::vfunc()" << endl; } }; class CChild: public CBase { public: vfunc() { cout << "CChild::vfunc()" << endl; } }; int main() { CBase *pBase = new CBase; ((CChild*)pBase)->vfunc(); // !!! important delete pBase; return 0; } The output is: CBase::vfunc() But I want to see: CChild::vfunc() Explicit ((CChild*)pBase) casts to type "CChild*". So why to call derived vfunc() I need replace "important" string with: ((CChild*)pBase)->CChild::vfunc(); That's not how it works - this is: CBase *pBase = new CChild; pBase->vfunc();