virtual-functions

Question of using static_cast on “this” pointer in a derived object to base class

末鹿安然 提交于 2019-12-22 07:20:14
问题 this is an example taken from Effective C++ 3ed , it says that if the static_cast is used this way, the base part of the object is copied, and the call is invoked from that part. I wanted to understand what is happening under the hood, will anyone help? class Window { // base class public: virtual void onResize() { } // base onResize impl }; class SpecialWindow: public Window { // derived class public: virtual void onResize() { // derived onResize impl; static_cast<Window>(*this).onResize();

Is an object allowed to legally change its type during its lifetime in C++?

Deadly 提交于 2019-12-22 04:10:39
问题 I have this code: class Class { public: virtual void first() {}; virtual void second() {}; }; Class* object = new Class(); object->first(); object->second(); delete object; that I compile with Visual C++ 10 with /O2 and have this disassembly: 282: Class* object = new Class(); 00403953 push 4 00403955 call dword ptr [__imp_operator new (4050BCh)] 0040395B add esp,4 0040395E test eax,eax 00403960 je wmain+1Ch (40396Ch) 00403962 mov dword ptr [eax],offset Class::`vftable' (4056A4h) 00403968 mov

Changing the params modifier in a method override

╄→гoц情女王★ 提交于 2019-12-22 03:57:04
问题 I'm aware that a params modifier (which turns in one parameter of array type into a so-called "parameter array") is specifically not a part of the method signature. Now consider this example: class Giraffid { public virtual void Eat(int[] leaves) { Console.WriteLine("G"); } } class Okapi : Giraffid { public override void Eat(params int[] leaves) { Console.WriteLine("O"); } } This compiles with no warnings. Then saying: var okapi = new Okapi(); okapi.Eat(2, 4, 6); // will not compile! gives an

in c++ when subclassing why sometimes need to add virtual keyword to overridden function?

[亡魂溺海] 提交于 2019-12-22 01:33:34
问题 Why do I sometimes see in C++ examples when talking about subclassing / inheritance, the base class has virtual keyword and sometimes the overridden function has also the virtual keyword, why it's necessary to add to the subclass the virtual key word sometimes? For example: class Base { Base(){}; virtual void f() ...... } }; class Sub : public Base { Sub(){}; virtual void f() ...new impl of f() ... } }; 回答1: It is not necessary, but it helps readability if you only see the derived class

Virtual Functions in C++ and Java

邮差的信 提交于 2019-12-21 17:52:30
问题 I have been reading about Virtual functions and found, VF are used in polymorphism of an inherited class. So , if a class and derived class both have the same function name, VF binds the appropriate function to the function call. i.e. If the function in question is designated virtual in the base class then the derived class's function would be called. If it is not virtual, the base class's function would be called. In Java by default: all functions are Virtual C++: Non-virtual and can be made

What are the uses of pure virtual functions in C++?

柔情痞子 提交于 2019-12-20 09:46:22
问题 I'm learning about C++ in a class right now and I don't quite grok pure virtual functions. I understand that they are later outlined in a derived class, but why would you want to declare it as equal to 0 if you are just going to define it in the derived class? 回答1: Briefly, it's to make the class abstract, so that it can't be instantiated, but a child class can override the pure virtual methods to form a concrete class. This is a good way to define an interface in C++. 回答2: This forces a

How to use virtual functions to achieve a polymorphic behavior in C++?

两盒软妹~` 提交于 2019-12-20 04:55:25
问题 I am new to these important features of C++, i have already read a few question/answers on these topics here and googled a few docs. But i am still confused with this... It would be great if some one can advice me some good online tutorial or book chapter which takes this concepts easy and slow and starts it from the basic. Also, if some one knows some on-hand exercise material that would be great. 回答1: Here's the best explanation of polymorphism that I've ever heard: There are many animals

How do we call a virtual method from another method in the base class even when the current instance is of a derived-class?

喜夏-厌秋 提交于 2019-12-19 20:15:29
问题 How do we call a virtual method from another method in the base class even when the current instance is of a derived-class? I know we can call Method2 in the Base class from a method in the Derived class by using base.Method2() but what I want to do is calling it from the other virtual method in the Base class. Is it possible? using System; namespace ConsoleApplication1 { class Program { static void Main( string[] args ) { Base b = new Derived( ); b.Method1( ); } } public class Base { public

C#: Any way to skip over one of the base calls in polymorphism?

狂风中的少年 提交于 2019-12-19 15:41:11
问题 class GrandParent { public virtual void Foo() { ... } } class Parent : GrandParent { public override void Foo() { base.Foo(); //Do additional work } } class Child : Parent { public override void Foo() { //How to skip Parent.Foo and just get to the GrandParent.Foo base? //Do additional work } } As the code above shows, how can I have the Child.Foo() make a call into GrandParent.Foo() instead of going into Parent.Foo()? base.Foo() takes me to the Parent class first. 回答1: Your design is wrong if

What does the virtual keyword mean when overriding a method?

大城市里の小女人 提交于 2019-12-19 05:17:14
问题 What does the keyword virtual do when overriding a method? I'm not using it and everything works fine. Does every compiler behave the same in this regard? Should I use it or not? 回答1: You cannot override a member function without it. You can only hide one. struct Base { void foo() {} }; struct Derived : Base { void foo() {} }; Derived::foo does not override Base::foo ; it simply hides it because it has the same name, such that the following: Derived d; d.foo(); invokes Derived::foo . virtual