virtual-functions

C++ virtual function not called in subclass

穿精又带淫゛_ 提交于 2019-12-05 23:05:21
问题 Consider this simple situation: A.h class A { public: virtual void a() = 0; }; B.h #include <iostream> class B { public: virtual void b() {std::cout << "b()." << std::endl;}; }; C.h #include "A.h" #include "B.h" class C : public B, public A { public: void a() {std::cout << "a() in C." << std::endl;}; }; int main() { B* b = new C(); ((A*) b)->a(); // Output: b(). A* a = new C(); a->a(); // Output:: a() in C. return 0; } In other words: - A is a pure virtual class. - B is a class with no super

replacement for “fvtable-gc” in GCC

蹲街弑〆低调 提交于 2019-12-05 18:18:33
Is there any replacement for 'fvtable-gc' options in GCCv4.7.1 (it was supported in GCCv3.x)? I want to remove unused virtual functions during linkage process. fvtable-gc Emit special relocations for vtables and virtual function references so that the linker can identify unused virtual functions and zero out vtable slots that refer to them. This is most useful with -ffunction-sections and -Wl,--gc-sections, in order to also discard the functions themselves. Looks like the feature was too buggy, so it has been removed several years ago. I don't think there is an equivalent replacement. Though I

Why should virtual functions not be used excessively?

偶尔善良 提交于 2019-12-05 14:31:24
问题 I just read that we should not use virtual function excessively. People felt that less virtual functions tends to have fewer bugs and reduces maintenance. What kind of bugs and disadvantages can appear due to virtual functions? I'm interested in context of C++ or Java. One reason I can think of is virtual function may be slower than normal functions due to v-table lookup. 回答1: You've posted some blanket statements that I would think most pragmatic programmers would shrug off as being

Using reflection to override virtual method tables in C#

别等时光非礼了梦想. 提交于 2019-12-05 14:25:19
Is there a way to change the virtual methods tables in C#? like change where a virtual method is pointing? class A { public virtual void B() { Console.WriteLine("B"); } } class Program { public static void MyB(A a) { Console.WriteLine("MyB"); } public static void Main(string[] Args) { A a = new A(); // Do some reflection voodoo to change the virtual methods table here to make B point to MyB a.B(); // Will print MyB } } Take a look at LinFu . On Linfu's author's Blog there's an example of using LinFu.AOP to intercept and change calls even to methods of classes that you don't control directly.

How to get every virtual function index just as the compiler does?

淺唱寂寞╮ 提交于 2019-12-05 12:46:51
Is there some plugin or tool which can read a .h file (or simply modify Intellisense itself) and spit out every function and it's virtual function table index? There's a pattern which I have yet to figure out having to do with polymorphism, and it gets 5x harder when you start to have 5 classes or more deriving from each other. No matter what, though, the MSVC++ compiler always spits out the correct virtual function table index when it compiles the virtual function call from C++ to Assembly. There has to be a better way to get that index without loading, break-pointing, reading the offset, and

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

戏子无情 提交于 2019-12-05 10:51: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(); // cast *this to Window, // then call its onResize; // this doesn't work! // do SpecialWindow- } //

Can a virtual function be overridden by a non-virtual function?

…衆ロ難τιáo~ 提交于 2019-12-05 08:10:28
In this code: class Base { public: virtual void method() = 0; }; class Derived1 : public Base{ public: virtual void method() override { } }; class Derived2 : public Base{ public: void method() override { } }; Is there any difference between Derived1 and Derived2 ? From section 10.3 Virtual functions of the c++11 standard (draft n3337) point 2: If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and refqualifier (or absence of same) as

overriding a function c#

时光毁灭记忆、已成空白 提交于 2019-12-05 06:08:35
I was just trying to master the concept of virtual function using a console app. I noticed as soon as I override a base class function, return baseclassname.functionname(parameters) gets inserted in my function body automatically. Why does this happen? class advanc { public virtual int calc (int a , int b) { return (a * b); } } class advn : advanc { public override int calc(int a, int b) { //automatically inserted return base.calc(a, b); } } By overriding a virtual function you expand the functionality of your base class and then call the base class for the 'base functionality'. If you would

C++: is a class with virtual base but without virtual functions polymorphic and has VTable?

两盒软妹~` 提交于 2019-12-05 05:55:47
Consider the following code: #include <iostream> #include <typeinfo> #include <type_traits> using namespace std; struct A { int data; }; struct B1 : A {}; struct B2 : virtual A {}; struct Base1 : virtual A {}; struct Base2 : virtual A {}; struct Derived : Base1, Base2 {}; int main() { cout << sizeof(B1) << endl; cout << sizeof(B2) << endl; cout << sizeof(Derived) << endl; cout << std::is_polymorphic<B1>::value << endl; cout << std::is_polymorphic<B2>::value << endl; cout << std::is_polymorphic<Derived>::value << endl; return 0; } On my system it prints 4 8 12 0 0 0 Which means that none of

Overloaded virtual function call resolution

落爺英雄遲暮 提交于 2019-12-05 05:16:08
Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc class Bbase{ public: virtual void f(Abase* a); virtual void f(A1* a); virtual void f(A2* a); }; class B1:public Bbase{ public: void f(A1* a); }; class B2:public Bbase{ public: void f(A2* a); }; int main(){ A1* a1=new A1(); A2* a2=new A2(); Bbase* b1=new B1(); Bbase* b2=new B2(); b1->f(a1); // calls B1::f(A1*), ok b2->f(a2); // calls B2::f(A2*), ok b2->f(a1); // calls Bbase::f(A1*), ok b1->f(a2); // calls Bbase::f(A2*), no- want B1::f(A1*)! } I'm interested to know why C++ chooses to resolve