virtual-functions

c++ virtual function vs member function pointer (performance comparison)

╄→尐↘猪︶ㄣ 提交于 2020-02-02 14:30:51
问题 Virtual function calls can be slow due to virtual calls requiring an extra indexed deference to the v-table, which can result in a data cache miss as well as an instruction cache miss... Not good for performance critical applications. So I have been thinking of a way to overcome this performance issue of virtual functions yet still having some of the same functionality that virtual functions provide. I am confident that this has been done before, but I devised a simple test that allows the

c++ virtual function vs member function pointer (performance comparison)

僤鯓⒐⒋嵵緔 提交于 2020-02-02 14:30:07
问题 Virtual function calls can be slow due to virtual calls requiring an extra indexed deference to the v-table, which can result in a data cache miss as well as an instruction cache miss... Not good for performance critical applications. So I have been thinking of a way to overcome this performance issue of virtual functions yet still having some of the same functionality that virtual functions provide. I am confident that this has been done before, but I devised a simple test that allows the

When does the vptr (pointing to vtable) get initialized for a polymorphic class?

风流意气都作罢 提交于 2020-01-31 02:21:30
问题 This is not about "When VTABLE is created?". Rather, when the VPTR should be initialized? Is it at the beginning/end of the constructor or before/after the constructor? A::A () : i(0), j(0) -->> here ? { -->> here ? //... -->> here ? } 回答1: The machinery for virtual calls (usually a v-table, but doesn't need to be) is set up during the ctor-initializer , after construction of base subobjects and before construction of members. Section [class.base.init] decrees: Member functions (including

Does final imply override?

风格不统一 提交于 2020-01-30 14:20:53
问题 As I understand it, the override keyword states that a given declaration implements a base virtual method, and the compilation should fail if there is no matching base method found. My understanding of the final keyword is that it tells the compiler that no class shall override this virtual function. So is override final redundant? It seems to compile fine. What information does override final convey that final does not? What is the use case for such a combination? 回答1: final does not require

Why does g++ store class names in the compiled binary?

痴心易碎 提交于 2020-01-14 07:12:14
问题 I noticed that If I run strings on my program which was compiled by g++ the output contains the names of various classes that it uses. The program was compiled with -O3 and without -g or -p , and the class names are still present when I strip the binary. I was wondering why it is necessary for g++ to store this information in the binary? The class names that are present all seem to be classes that use virtual functions, so I suspect this is something to do with it. 回答1: This might have

What are the dangers of making a method virtual?

倾然丶 夕夏残阳落幕 提交于 2020-01-11 05:14:12
问题 I've been doing some mocking with RhinoMocks and it requires that mocked methods be made virtual. This is fine except we have a custom framework which contains the methods that I want to mock which are currently not marked as virtual. I can't forsee any problem with making these methods virtual but I was wondering what are some potential dangers of making methods virtual that I should look out for? 回答1: Actually it can be very problematic if the method is not designed to be overridden and

Hooking/Detour Virtual Functions

跟風遠走 提交于 2020-01-05 18:25:02
问题 I've been trying to properly hook/detour a virtual function in a class object, and I've had success in terms of having a different function called, but I must be doing something that's incorrect in terms of how the this keyword is passed to the function. I read an article about hooking D3D functions in a similar fashion, and it mentioned that the compiler will turn a function such as int Class::method(int) into int method(Class* this, int) , but if I replace the address in the vtable with a

Why can't one use scope resolution with member pointer dereference?

馋奶兔 提交于 2020-01-05 06:52:22
问题 Consider a simple example: struct FooParent { virtual void bar() { } }; struct Foo: FooParent { void bar() { } }; int main() { Foo foo; void (Foo::*foo_member)() = &FooParent::bar; //(foo.*FooParent::foo_member)(); foo.FooParent::bar(); } As you can see one can use scope resolution on the foo object when calling bar member function while there is no way to explicitly declare the scope for member function pointer. I accept that the syntax should be prohibited when using ->* as the operator can

Conflicting type attributes specified for virtual destructor

懵懂的女人 提交于 2020-01-05 01:52:34
问题 The following extract was previously compiling under Borland C++, MSVC and OpenWatcom: class aaa { virtual _fastcall ~aaa(); }; class bbb:public aaa { }; It doesn't compile under gcc/g++ (MinGW 4.8.0). Error: probz.cpp:7:7: error: conflicting type attributes specified for 'virtual bbb::~bbb()' class bbb:public aaa { ^ probz.cpp:3:20: error: overriding 'virtual aaa::~aaa()' virtual _fastcall ~aaa()=0;///can't be abstract ^ Obviously, there is no bbb::~bbb()! EDIT The actual class hierarchy is

Calling class T's implementation of pure virtual from T constructor without qualification?

本小妞迷上赌 提交于 2020-01-04 05:28:20
问题 Considering that a virtual call of a T member function (directly or indirectly) from a constructor of a class T , can at most go down to T 's implementation, does the following code, with unqualified call , have Undefined Behavior or not? Note, to avoid noise: if you believe that member functions are not called virtually when invoked from a constructor, then please don't answer or comment here, but raise that issue in a separate SO question. Thank you. struct Baze { virtual void foo();