virtual

How Vtable of Virtual functions work

点点圈 提交于 2019-12-19 08:54:51
问题 I have a small doubt in Virtual Table, whenever compiler encounters the virtual functions in a class, it creates Vtable and places virtual functions address over there. It happens similarly for other class which inherits. Does it create a new pointer in each class which points to each Vtable? If not how does it access the Virtual function when the new instance of derived class is created and assigned to Base PTR? 回答1: Each time you create a class that contains virtual functions, or you derive

Why is the virtual keyword needed?

佐手、 提交于 2019-12-19 07:34:34
问题 In other words, why doesn't the compiler just "know" that if the definition of a function is changed in a derived class, and a pointer to dynamically allocated memory of that derived class calls the changed function, then that function in particular should be called and not the base class's? In what instances would not having the virtual keyword work to a programmer's benefit? 回答1: virtual keyword tells the compiler to implement dynamic dispatch .That is how the language was designed. Without

C#的一些关键字的总结

百般思念 提交于 2019-12-19 06:56:24
今天突然有一种整理一下C#关键字的冲动,就转化为行动了! C# 关键字完整列表 abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum ecent explicit extern false finally fixed float for foreach get goto if implicit in int interface internal is lock long namespace new null object out override partial private protected public readonly ref return sbyte sealed set short sizeof stackalloc static struct switch this throw true try typeof uint ulong unchecked unsafe ushort using value virtual volatile volatile void where while yield 其中有几个比较容易弄错的 关键字 描 述 abstract 可以和类、方法、属性

Virtual camera/direct show filter for network stream

Deadly 提交于 2019-12-19 04:55:23
问题 i'm working with Flash Live Encoder. It's using camera for streaming video. Support forum say's that i can create custom direct show filter and stream data that i need. I cann't understand how direct show filter will display in the source list of the live encoder. I've tryed to use some commercial virtual camera and it work's fine, but it cann't use source from network stream. Summary. I have a several network streams. I think that i must to create virtual camera for each one. But if i find

Access specifiers and virtual functions

纵饮孤独 提交于 2019-12-19 04:47:31
问题 What are the rules for accessibility when virtual functions are declared under 3 different access specifiers specified by C++(public, private, protected) What is the significance of each? Any simple code examples to explain the concept will be highly useful. 回答1: Access specifiers apply in the same way as they would to any other name during name lookup. The fact that the function is virtual does not matter at all. There is a common mistake which sometimes happens with respect to virtual

Is making a function template specialization virtual legal?

房东的猫 提交于 2019-12-18 19:43:32
问题 In C++, a function template specialization is supposed to act exactly like a normal function. Does that mean that I can make one virtual? For example: struct A { template <class T> void f(); template <> virtual void f<int>() {} }; struct B : A { template <class T> void f(); template <> virtual void f<int>() {} }; int main(int argc, char* argv[]) { B b; A& a = b; a.f<int>(); } Visual Studio 2005 gives me the following error: fatal error C1001: An internal error has occurred in the compiler.

More about Virtual / new…plus interfaces!

☆樱花仙子☆ 提交于 2019-12-18 13:36:33
问题 Yesterday I posted a question about the new/virtual/override keywords, and i learned a lot from your answers. But still i remain with some doubts. In between all the "boxes", i lost touch with what's really happening to the type's method tables. For instance: interface I1 { void Draw(); } interface I2 { void Draw(); } class A : I1, I2 { public void Minstance() { Console.WriteLine("A::MInstance"); } public virtual void Draw() { Console.WriteLine("A::Draw"); } void I2.Draw() { Console.WriteLine

vftable performance penalty vs. switch statement

元气小坏坏 提交于 2019-12-18 10:33:16
问题 C++ question here. I have a system where I'm going to have hundreds of mini-subclasses of a given superclass. They all will have a "foo" method that does something. Or... I'm going to have one class with an integer called "type" and use a giant switch statement to decide what to do when I foo. Performance is a huge consideration here. Extremely important. The question is, what are the performance benefits/penalties of using a switch statement vs. letting C++ do it via the vftable? If I have

重载.覆盖.隐藏

谁说胖子不能爱 提交于 2019-12-18 04:38:50
a.成员函数被重载overload的特征: (1)相同的范围( 在同一个类中 ); (2)函数名字相同; (3)参数不同; (4)virtual 关键字可有可无。 b.覆盖override(重写)是指派生类函数覆盖基类函数,特征是: (1)不同的范围( 分别位于派生类与基类 ); (2)函数名字相同; (3)参数相同; (4)基类函数必须有virtual 关键字。 c. “隐藏”是指派生类的函数屏蔽了与其同名的基类函数 ,规则如下: (1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆)。 (2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual 关键字。此时,基类的函数被隐藏(注意别与覆盖混淆) 它们之间最为重要的区别就是: 覆盖的函数是多态的,是存在于vtbl之中的函数才能构成"覆盖"的关系,而隐藏的函数都是一般的函数,不支持多态,在编译阶段就已经确定下来了. 也可参考: 重载和重写的区别: (1)范围区别:重写和被重写的函数在不同的类中,重载和被重载的函数在同一类中。 (2)参数区别:重写与被重写的函数参数列表一定相同,重载和被重载的函数参数列表一定不同。 (3)virtual的区别:重写的基类必须要有virtual修饰,重载函数和被重载函数可以被virtual修饰

it is possible to change return type when override a virtual function in C++?

亡梦爱人 提交于 2019-12-18 04:16:19
问题 I encounter a problems about override virtual functions, in fact,it is about hessian (a web service protocol). it has a base class Object, and some derived classes : Long,Int,String,...,all derived classes has a no-virtual function "value" class Object { ... }; class Long :public Object { ... public: typedef long long basic_type; basic_type value(){return value_;} private: basic_type value_; ... }; class Int :public Object { ... public: typedef int basic_type; basic_type value(){return value_