virtual

C++ : implications of making a method virtual

大兔子大兔子 提交于 2019-12-01 11:06:05
Should be a newbie question... I have existing code in an existing class, A, that I want to extend in order to override an existing method, A::f(). So now I want to create class B to override f(), since I don't want to just change A::f() because other code depends on it. To do this, I need to change A::f() to a virtual method, I believe. My question is besides allowing a method to be dynamically invoked (to use B's implementation and not A's) are there any other implications to making a method virtual? Am I breaking some kind of good programming practice? Will this affect any other code trying

How to write a driver for virtual printer

强颜欢笑 提交于 2019-12-01 09:34:30
问题 I want to write a driver for virtual printer (in C++ or Java) (As explained in How to create a virtual printer in Windows?) I would redirect the file to a file server (again as explained in http://www.colorpilot.com/VirtualPrinterForCSharp2.html. What are the fundamental building blocks of a printer driver ? I have read about spooler and print monitor but I am not sure that these are all that I have to know! Any guide or suggestion appreciated in advance! Regards 回答1: Most print redirection

接口

强颜欢笑 提交于 2019-12-01 08:48:45
接口只是对一组方法签名进行了统一命名,这些方法不提供任何实现。类通过指定接口名称来继承接口,而且必须显示实现接口方法,否则CLR 会认为此类型定义无效。 类继承有一个重要的特点,凡是能使用基类型实例的地方,都能使用派生类型的实例。类似地,接口继承的一个重要特点是,凡是能使用具名接口的类型的实例的地方,都能使用实现了接口的一个类型的实例。 继承接口 C# 编译器要求将实现接口的方法标记为 public。CLR 要求将接口方法标记为virtual。 不将方法显示标记为 virtual,编译器会将它们标记为virtual和sealed:这会阻止派生类重写接口方法。 将方法显示标记为 virtual,编译器就会将该方法标记为virtual,使派生类能重写它。 派生类不能重写sealed 接口方法。但派生类可重新继承同一个接口,并为接口方法提供自己的实现。在对象上调用接口方法时,调用的是方法在该对象类型中的实现。 using System; public static class Program { public static void Main() { /************************* First Example *************************/ Base b = new Base(); // Calls Dispose by using b's

How Vtable of Virtual functions work

自作多情 提交于 2019-12-01 06:53:52
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? Each time you create a class that contains virtual functions, or you derive from a class that contains virtual functions, the compiler creates a unique VTABLE for that class. If you

When virtual doesn't work

天涯浪子 提交于 2019-12-01 06:44:35
问题 I have a weird error in my C++ classes at the moment. I have an ActiveX wrapper class (as part of wxWidgets) that i added a new virtual function to. I have another class that inherits from the ActiveX one (wxIEHtmlWin) however the ActiveX class always calls its own function instead of the one in wxIEHtmlWin which overrides it. I can't work out why this is happening. I made the function pure virtual and now the program crashes when it does the function call but compiles fine otherwise. Is

Is there no way to upcast into an abstract class and not modify it each time a class is derived from it?

北城余情 提交于 2019-12-01 06:17:33
问题 #include<iostream> using namespace std; class Abs { public: virtual void hi()=0; }; class B:public Abs { public: void hi() {cout<<"B Hi"<<endl;} void bye() {cout<<"B Bye"<<endl;} }; class C:public Abs { public: void hi() {cout<<"C Hi"<<endl;} void sayonara() {cout<<"C Sayonara"<<endl;} }; int main() { Abs *bb=new B; bb->bye(); Abs *cc=new C; cc->sayonara(); }//main The compiler says test2.cpp: In function ‘int main()’: test2.cpp:26: error: ‘class Abs’ has no member named ‘bye’ test2.cpp:28:

Why is the virtual keyword needed?

自闭症网瘾萝莉.ら 提交于 2019-12-01 05:54:45
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? virtual keyword tells the compiler to implement dynamic dispatch .That is how the language was designed. Without such an keyword the compiler would not know whether or not to implement dynamic dispatch. The downside of

Capturing virtual screen (all monitors)

☆樱花仙子☆ 提交于 2019-12-01 05:20:43
I am trying to get a screenshot of the whole virtual screen. This means, an image of not just the primary screen, but every screen connected to the computer. Is there a way to do that? I tried using this, but it didn't work: Bitmap b = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height); Graphics g = Graphics.FromImage(b); this.Size = new Size(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height); g.CopyFromScreen(0, 0, 0, 0, b.Size); The documentation says: Graphics.CopyFromScreen(Int32, Int32, Int32, Int32, Size) : Performs a bit

How to enforce a static member on derived classes?

江枫思渺然 提交于 2019-12-01 04:46:55
I have a base class, Primitive , from which I derive several other classes-- Sphere , Plane , etc. Primitive enforces some functionality, e.g intersect() , on its subclasses through pure virtual functions. The computation of intersect depends on instance data, so it makes sense to have it as a member method. My problem arises in the following: I want every derived instance to be able to identify its type, say through a std::string type() member method. As all instances of the same class will return the same type, it makes sense to make type() a static method. As I also want every Primitive