virtual-functions

Fastest implementation of simple, virtual, observer-sort of, pattern in c++?

穿精又带淫゛_ 提交于 2019-12-18 04:23:21
问题 I'm working my arse off trying to implement an alternative for vtables using enums and a ton of macro magic that's really starting to mess with my brain. I'm starting to think i'm not walking the right path since the code is getting uglier and uglier, and will not be fit for production by any means. How can the pattern of the following code be implemented with the least amount of redirection/operations? It has to be done in standard c++, up to 17. class A{ virtual void Update() = 0; // A is

C++ Pointer to virtual function

霸气de小男生 提交于 2019-12-17 23:29:50
问题 If you have a struct like this one struct A { void func(); }; and a reference like this one A& a; you can get a pointer to its func method like this: someMethod(&A::func); Now what if that method is virtual and you don't know what it is at run-time? Why can't you get a pointer like this? someMethod(&a.func); Is it possible to get a pointer to that method? 回答1: Pointers to members take into account the virtuality of the functions they point at. For example: #include <iostream> struct Base {

Why would a virtual function be private?

拥有回忆 提交于 2019-12-17 21:54:43
问题 I just spotted this in some code: class Foo { [...] private: virtual void Bar() = 0; [...] } Does this have any purpose? (I am trying to port some code from VS to G++, and this caught my attention) 回答1: See this Herb Sutter article as to why you'd want to do such a thing. 回答2: This is a pure virtual function that happens to be private. This makes it so that a derived class must implement the method. In this case Bar. I think you may be confused b/c this is done to create "interfaces" in C++

When should you not use virtual destructors?

蓝咒 提交于 2019-12-17 17:28:52
问题 Is there ever a good reason to not declare a virtual destructor for a class? When should you specifically avoid writing one? 回答1: There is no need to use a virtual destructor when any of the below is true: No intention to derive classes from it No instantiation on the heap No intention to store in a pointer of a superclass No specific reason to avoid it unless you are really so pressed for memory. 回答2: To answer the question explicitly, i.e. when should you not declare a virtual destructor. C

Public virtual function derived private in C++

╄→гoц情女王★ 提交于 2019-12-17 15:33:41
问题 I was trying to figure out what happens when a derived class declares a virtual function as private. The following is the program that I wrote #include <iostream> using namespace std; class A { public: virtual void func() { cout<<"A::func called"<<endl; } private: }; class B:public A { public: B() { cout<<"B constructor called"<<endl; } private: void func() { cout<<"B::func called"<<endl; } }; int main() { A *a = new B(); a->func(); return 0; } Surprisingly (for me) the output was: B

Virtual table/dispatch table

我的未来我决定 提交于 2019-12-17 10:48:05
问题 From what I know of CPP, each class has its own vtable. However this wikipedia link mentions: An object's dispatch table will contain the addresses of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's dispatch table. The dispatch table is the same for all objects belonging to the same class, and is therefore typically shared between them. Can someone please shed some light. Thanks ! 回答1: It's sometimes easier to understand

Multiple inheritance + virtual function mess

此生再无相见时 提交于 2019-12-17 10:45:30
问题 I have a diamond multiple inheritance scenario like this: A / \ B C \ / D The common parent, A, defines a virtual function fn(). Is it possible for both B and C to define fn() ? If it is, then the next question is - can D access both B and C's fn() without disambiguation? I'm assuming there is some syntax for this.. And is it possible for D to do that without knowing specifically who are B and C? B and C can be replaces by some other classes and I want the code in D to be generic. What I'm

Safely override C++ virtual functions

我只是一个虾纸丫 提交于 2019-12-17 10:12:53
问题 I have a base class with a virtual function and I want to override that function in a derived class. Is there some way to make the compiler check if the function I declared in the derived class actually overrides a function in the base class? I would like to add some macro or something that ensures that I didn't accidentally declare a new function, instead of overriding the old one. Take this example: class parent { public: virtual void handle_event(int something) const { // boring default

What's the point of a final virtual function?

拈花ヽ惹草 提交于 2019-12-17 09:37:10
问题 Wikipedia has the following example on the C++11 final modifier: struct Base2 { virtual void f() final; }; struct Derived2 : Base2 { void f(); // ill-formed because the virtual function Base2::f has been marked final }; I don't understand the point of introducing a virtual function and immediately marking it as final. Is this simply a bad example, or is there more to it? 回答1: Typically final will not be used on the base class' definition of a virtual function. final will be used by a derived

Use-cases of pure virtual functions with body?

。_饼干妹妹 提交于 2019-12-17 08:24:27
问题 I recently came to know that in C++ pure virtual functions can optionally have a body. What are the real-world use cases for such functions? 回答1: The classic is a pure virtual destructor: class abstract { public: virtual ~abstract() = 0; }; abstract::~abstract() {} You make it pure because there's nothing else to make so, and you want the class to be abstract, but you have to provide an implementation nevertheless, because the derived classes' destructors call yours explicitly. Yeah, I know,