virtual

Can I get polymorphic behavior without using virtual functions?

丶灬走出姿态 提交于 2019-11-27 14:39:28
Because of my device I can't use virtual functions. Suppose I have: class Base { void doSomething() { } }; class Derived : public Base { void doSomething() { } }; // in any place { Base *obj = new Derived; obj->doSomething(); } the obj->doSomething() will call just the Base::doSomething() Is there a way with Base *obj , to call the doSomething of the Derived ? I know I can just put a virtual before doSomething() of Base it solve the problem, but I'm limited by my device, the compiler doesn't support it. paxos1977 You could down cast the base class pointer to the derived class and call the

Confusion about virtual/new/override

房东的猫 提交于 2019-11-27 14:07:25
问题 I am a bit confused about the virtual / new / override thing. Here's an example: class A { public virtual void mVVirtual() { Console.WriteLine("A::mVVirtual"); } } class B : A { public virtual void mVVirtual() { Console.WriteLine("B::mVVirtual"); } } class C : B { public override void mVVirtual() { Console.WriteLine("C::mVVirtual"); } } class Test { static void Main() { B b1 = new C(); b1.mVVirtual(); //C::mVVirtual ... I understand this A a2 = new C(); a2.mVVirtual(); //A::mVVirtual ... ???

Pointers to virtual member functions. How does it work?

梦想的初衷 提交于 2019-11-27 13:55:55
Consider the following C++ code: class A { public: virtual void f()=0; }; int main() { void (A::*f)()=&A::f; } If I'd have to guess, I'd say that &A::f in this context would mean "the address of A's implementation of f()", since there is no explicit seperation between pointers to regular member functions and virtual member functions. And since A doesn't implement f(), that would be a compile error. However, it isn't. And not only that. The following code: void (A::*f)()=&A::f; A *a=new B; // B is a subclass of A, which implements f() (a->*f)(); will actually call B::f. How does it happen? Here

Virtual Printer Driver for Windows

倖福魔咒の 提交于 2019-11-27 13:32:28
问题 can you please help me with the following questions... If I need a virtual printer that will convert a PostScript stream to a different format, do I have to implement a virtual printer from scratch or implement a rendering plug-in? The rendering plug-in seems to support only certain customizations. Also the data invariably goes to the spooler which is not needed in this case. If I implement a virtual printer driver does it completely replace the Microsoft PostScript Driver or the Microsoft

Virtual method tables

谁说我不能喝 提交于 2019-11-27 12:49:06
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 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 example, Wikipedia's description is not bad. The "virtual function table" or "virtual method table" is a list of

C# virtual static method

a 夏天 提交于 2019-11-27 12:00:16
问题 Why is static virtual impossible? Is C# dependent or just don't have any sense in the OO world? I know the concept has already been underlined but I did not find a simple answer to the previous question. 回答1: virtual means the method called will be chosen at run-time, depending on the dynamic type of the object. static means no object is necessary to call the method. How do you propose to do both in the same method? 回答2: Eric Lippert has a blog post about this, and as usual with his posts, he

How do virtual functions work in C# and Java?

♀尐吖头ヾ 提交于 2019-11-27 11:39:32
问题 How do the virtual functions work in C# and Java? Does it use same vtable and vpointer concept similar to C++ or is it something totally different? 回答1: How do virtual functions work in Java? Coding interviewers love this question. Yes. Although Java does NOT have a virtual keyword, Java has virtual functions and you can write them. In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a

sizeof class with int , function, virtual function in C++?

空扰寡人 提交于 2019-11-27 11:30:24
This is an online C++ test question, which has been done. #include<iostream> using namespace std; class A { }; class B { int i; }; class C { void foo(); }; class D { virtual void foo(); }; class E { int i ; virtual void foo(); }; class F { int i; void foo(); }; class G { void foo(); int i; void foo1(); }; class H { int i ; virtual void foo(); virtual void foo1(); }; int main() { cout <<"sizeof(class A) : " << sizeof(A) << endl ; cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ; cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ; cout <<

Why are private virtual methods illegal in C#?

假装没事ソ 提交于 2019-11-27 11:27:18
Coming from a C++ background, this came as a surprise to me. In C++ it's good practice to make virtual functions private. From http://www.gotw.ca/publications/mill18.htm : "Guideline #2: Prefer to make virtual functions private." I also quote Eric Lippert's blog, from Knights-knaves-protected-and-internal : Private virtual methods are illegal in C#, which irks me to no end. I would totally use that feature if we had it. I understand that in C#, you wouldn't be able to override a private virtual method in a derived (but not nested) class. Why is this the case? In C++ the access specifier has

What are the performance implications of marking methods / properties as virtual?

瘦欲@ 提交于 2019-11-27 11:05:15
Question is as stated in the title: What are the performance implications of marking methods / properties as virtual? Note - I'm assuming the virtual methods will not be overloaded in the common case; I'll usually be working with the base class here. Virtual functions only have a very small performance overhead compared to direct calls. At a low level, you're basically looking at an array lookup to get a function pointer, and then a call via a function pointer. Modern CPUs can even predict indirect function calls reasonably well in their branch predictors, so they generally won't hurt modern