virtual

【转】C#虚方法virtual详解

馋奶兔 提交于 2019-12-04 07:59:55
转: https://www.cnblogs.com/zhaoshujie/p/10502404.html 在C++、Java等众多OOP语言里都可以看到virtual的身影,而C#作为一个完全面向对象的语言当然也不例外。 虚拟函数从C#的程序编译的角度来看,它和其它一般的函数有什么区别呢?一般函数在编译时就静态地编译到了执行文件中,其相对地址在程序运行期间是不发生变化的,也就是写死了的!而虚函数在编译期间是不被静态编译的,它的相对地址是不确定的,它会根据运行时期对象实例来动态判断要调用的函数,其中那个申明时定义的类叫申明类,那个执行时实例化的类叫实例类。 如:飞禽 bird = new 麻雀(); 那么飞禽就是申明类,麻雀是实例类。 具体的检查的流程如下 : 1、当调用一个对象的函数时,系统会直接去检查这个对象申明定义的类,即申明类,看所调用的函数是否为虚函数; 2、如果不是虚函数,那么它就直接执行该函数。而如果有virtual关键字,也就是一个虚函数,那么这个时候它就不会立刻执行该函数了,而是转去检查对象的实例类。 3、在这个实例类里,他会检查这个实例类的定义中是否有重新实现该虚函数(通过override关键字),如果是有,那么OK,它就不会再找了,而马上执行该实例类中的这个重新实现的函数。而如果没有的话,系统就会不停地往上找实例类的父类,并对父类重复刚才在实例类里的检查

How to write a virtual printer driver for Mac OSX [closed]

▼魔方 西西 提交于 2019-12-04 07:59:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 months ago . I need to write a "virtual printer driver" for OSX, so that when the user presses Command+P to open the Print dialog, he sees my virtual printer...which will be used to generate files of various types, instead of physically printing to paper. I'm new to the subject, I looked around a bit but found nothing. Do

Simulate or Virtual Twain source for Twain development [closed]

做~自己de王妃 提交于 2019-12-04 06:33:52
Closed. This question is off-topic. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . For developing a web based scan solution, I would love to test it on Windows or Mac without actually hooking up a scanner to my box. So is there a program/tool that emulates or gives me a virtual twain source with some default image data? The Twain sample driver that comes along with the twain sample application up on source forge should have what you're looking for. http://sourceforge.net/projects

C++ virtual function from constructor [duplicate]

本秂侑毒 提交于 2019-12-04 06:00:57
问题 This question already has answers here : Calling virtual functions inside constructors (13 answers) Closed 3 years ago . Why the following example prints "0" and what must change for it to print "1" as I expected ? #include <iostream> struct base { virtual const int value() const { return 0; } base() { std::cout << value() << std::endl; } virtual ~base() {} }; struct derived : public base { virtual const int value() const { return 1; } }; int main(void) { derived example; } 回答1: Because base

What does it mean when “virtual” is in “class Foo : public virtual Bar” as opposed to “virtual void frob()”?

三世轮回 提交于 2019-12-04 05:39:14
What does it mean when "virtual" is in "class Foo : public virtual Bar" as opposed to "virtual void frob()"? For a given method there are 8 cases stemming from the presence or absence of virtual in the following three locations. A superclass's functions. The inheritance chain for this class. This classes functions. I think I understand how numbers 1 and 3 interact but number 2 seems redundant. Is it? What am I not understanding? That's virtual inheritance , you do it when you know you'll be doing multiple inheritance. That page goes into way more detail. 来源: https://stackoverflow.com/questions

Diamond inheritance and pure virtual functions

给你一囗甜甜゛ 提交于 2019-12-04 04:30:53
Imagine a standard diamond inheritance. Class A defines pure virtual function fx, class B defines implementation for fx, classes C and D do nothing with fx. When trying to call fx on instance of class D you'll get 'ambiguous function call' error although there is only one implementation of fx. This can be solved by B and C inheriting from A in virtual manner. Is it a correct solution for the problem? How exactly does virtual inheritance handle merging of virtual function tables? A--->B--->D \--->C------^ ... Note, Herb Sutter wrote 3 excellent articles about multiple inheritance (1) here , (2)

C++: Accessing Virtual Methods

折月煮酒 提交于 2019-12-04 04:22:43
问题 I'm trying to use the virtual method table to call functions by index in a class... Suppose we have the following code: class Base { public: Base() {} virtual ~Base() {} virtual Base* call_func(unsigned int func_number) { // Some way to call f_n } protected: virtual Base* f_1() const = 0; virtual Base* f_2() const = 0; virtual Base* f_3() const = 0; }; I've already implemented this using function arrays, if-statement and case-statement... so, Is there a a better approach to call methods using

Cloning C++ class with pure virtual methods

我只是一个虾纸丫 提交于 2019-12-04 03:24:21
问题 I have the following relation of classes. I want to clone the class Derived, but I get the error "cannot instantiate abstract class". How I can clone the derived class? Thanks. class Base { public: virtual ~Base() {} virtual Base* clone() const = 0; }; class Derived: public Base { public: virtual void func() = 0; virtual Derived* clone() const { return new Derived(*this); } }; 回答1: Only concrete classes can be instantiated. You have to redesign the interface of Derived in order to do cloning.

Speed of virtual call in C# vs C++

巧了我就是萌 提交于 2019-12-04 03:11:59
I seem to recall reading somewhere that the cost of a virtual call in C# is not as high, relatively speaking, as in C++. Is this true? If so - why? A C# virtual call has to check for “this” being null and a C++ virtual call does not. So I can’t see in generally why a C# virtual calls would be faster. In special cases the C# compiler (or JIT compiler) may be able to inline the virtual call better then a C++ compiler, as a C# compiler has access to better type information. The call method instruction may sometimes be slower in C++, as the C# JIT may be able to use a quicker instruction that only

WHy should virtual methods be explicitly overridden in C#?

时光怂恿深爱的人放手 提交于 2019-12-04 01:06:31
Why should virtual methods be explicitly overridden in C#? 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. SLaks If you don't add the override keyword, the method will be hidden (as if it had the new keyword), not overridden. For